Opening a password protected word file as read only

gruntingmonkey

Active Member
Joined
Mar 6, 2008
Messages
434
Office Version
  1. 365
Platform
  1. Windows
Hi, I can't figure out how to open the below file while selecting Read only when the password entry box comes up.

VBA Code:
If Dir(ConTemp) <> "" Then
  With wdApp
       .DisplayAlerts = wdAlertsNone
       .Visible = True
   Set wdDoc = wdApp.Documents.Add(ConTemp)

I have tried:
Application.DisplayAlerts = False
and
Set wdDoc = wdApp.Documents.Add(ConTemp, readonly:=true)
but neither of these have worked. I'm not sure what to try next! Has anyone got any ideas?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,
Try this:
VBA Code:
Set wdDoc = wdApp.Documents.Open(ConTemp, ReadOnly:=True, PasswordDocument:="123")
 
Upvote 0
Urgh! That works perfectly. Can I ask what the difference is between .add and .open as this seems to be the bit that stops it working?
 
Upvote 0
Glad it has helped!

In short - Open() is just opens the stored file of document, Add() is for creating in memory a new document.

Add() is the function for creating the new document based on a template, if template is omitted then the default is used.
Here is its definition:
Function Add([Template], [NewTemplate], [DocumentType], [Visible]) As Document

As you can see there is no ReadOnly and PasswordDocument parameters, which are present in the Open() function:
Function Open(FileName, [ConfirmConversions], [ReadOnly], [AddToRecentFiles], [PasswordDocument], [PasswordTemplate], [Revert], [WritePasswordDocument], [WritePasswordTemplate], [Format], [Encoding], [Visible], [OpenAndRepair], [DocumentDirection], [NoEncodingDialog], [XMLTransform]) As Document
 
Last edited:
Upvote 0
Right, great thanks. I was under the impression I had created these as templates but I can see now that they are just standard documents. That's been really helpful, thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top