Opening Password Protected Document with VBA

G

Greg Maxey

Perhaps my previous post was too involved so I will try again.

I have a document with an open password and a modify password. If I open
that document with the normal user interface and type an incorrect password
in either the Password open dialog or the Password modify dialog and then
click "OK" the applcation issues a notification of and incorrect password.
The document can not be opened and editied until both passwords are entered
correctly.

If I try to open that same document using the following code then the error
handler traps the incorrect password in the Password open dialog but when I
enter an incorrect password in the Password modify dialog and click OK
nothing happens. There is no error generated, the dialog remains in focus,
I can click on OK repeatedly and still nothing happens.

If I click "Cancel" in the Password modify dialog then an error is
generated. Also if I enter the wrong password, click OK, then enter the
correct password and click "Ok" the file opens but a new error 5485 is
generated.

Sub ScratchMacroII()
Dim oDoc As Word.Document
On Error GoTo Err_Password
Set oDoc = Documents.Open("C:\Test.doc") 'A document password protected for
opening and editing.
Exit Sub
Err_Password:
MsgBox Err.Number
End Sub

Can anyone duplicate this behaviour and offer an explanation?

Thanks.
 
G

Greg Maxey

Subject "Error Hanldling File Protection Passwords" posted in this group
8/2/2008 at 1:37 PM.
 
J

Jules

I can't I get asked to provide the password or open as a read only either
way the doc is accessible?
No help am I as usual!

Sorry
 
G

Greg Maxey

Jules:

Do you get the 5408 message if you type the wrong password in both the
Password Open and Password Modify popup dialogs?

For me in both Word2003 and Word2003 I get the 5408 message if I type the
wrong password in the Password Open popup dialog. However, if I type the
wrong password in the Password Modify popup dialog and click "OK" then
nothing happens ;-(
 
J

Jules

I only get one option dialogue? 5408 error when wrong password. Then it
closes dialogue box in Word 2003? Before I was using 2007 to do it as 2003
mode.
 
J

Jules

Greg,
I entered first password correctly.
Then next password incorrectly and dialogue box just stays on screen in Word
2003. It doesn't issue an error?
 
G

Greg Maxey

Did you save the document with a password to open and a different password
to modify? I admit that I didn't make that a condition earlier. Sorry.
 
G

Greg Maxey

Yes. That is what I see and that is the problem.
Greg,
I entered first password correctly.
Then next password incorrectly and dialogue box just stays on screen
in Word 2003. It doesn't issue an error?
 
G

Greg Maxey

Right. And if you enter the wrong password, click OK, then enter the
correct password and click OK. You will get error 5485 but the document
still opens.

I have been trying to put together and AddIn to automate processing a batch
of files located in a common folder. You can see it here:
http://gregmaxey.mvps.org/Process_Batch_Folder.htm

When protected documents are encountered the user has to enter the
passwords. I need to trap any errors that occur and ensure that "Read only"
documents are not opened for AutoProcessing.

On the Open Password dialog enter the wrong password and clicking OK results
in error 5408. Clicking Cancels results in error 4198.

I would expect the same errors for the same actions in the Modify Password
dialong, but as you are confirming that is not the case :-(
 
J

Jules

Yes, that's what happens.

Greg Maxey said:
Right. And if you enter the wrong password, click OK, then enter the
correct password and click OK. You will get error 5485 but the document
still opens.

I have been trying to put together and AddIn to automate processing a
batch of files located in a common folder. You can see it here:
http://gregmaxey.mvps.org/Process_Batch_Folder.htm

When protected documents are encountered the user has to enter the
passwords. I need to trap any errors that occur and ensure that "Read
only" documents are not opened for AutoProcessing.

On the Open Password dialog enter the wrong password and clicking OK
results in error 5408. Clicking Cancels results in error 4198.

I would expect the same errors for the same actions in the Modify Password
dialong, but as you are confirming that is not the case :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top