Error Hanlding File Protection Passwords

G

Greg Maxey

I am trying to finalize some code for opening password protected documents
with VBA and I am experiencing odd behaviour when I intentionally enter a
wrong password or cancel using the Password dialog. My objective is to
prevent a file from being opened as "Read Only."

I am posting a sanitized version of my code that should illustrated the
problem I am having. You will need to create and save a document as C:\Doc
1.doc or changes the Set oDoc statement. Doc 1 should have a password to
open and a password to modify.

As I step through the code and execute line 6, the Password dialog opens
prompting me for the open password. If I enter the correct password then
the Password dialog opens again prompting me for the modify password. If I
enter it correctly then the document opens and all works as expected.

Now if I enter the wrong open password an error is generated "5408" which I
handle as shown in my code. If I "Cancel" the open password dialog then
error "4198" is generated which I again can handle with the code shown.

The problems arise after I enter a corrrect open password and then start
working with the password to modify dialog.

1) If I click "Read Only" then the file opens read only as expected.

2). If I enter a wrong password and click "OK" nothing happens. I can
enter wrong passwords and click "OK" forever I suppose but nothing happens.

3) If I enter a wrong password and click "OK" once, or several times, or
simply click "OK" and then click "Read Only," an error "5485" is generated.
It may be ugly, but I have scrabbled together a way to handled that as well.

4. If I click "Cancel" then an error "5408" is generated. I would expect
and error "4198" like before but it isn't so. Of course this has is handled
like before and the user who intended to cancel is informed they entered an
invalid password.

My questions are:

1. Why doesn't clicking "OK" when the password field is blank or filled in
wrong generated an error like it does with the open password dialog?

2. How can I distinquish a error "5408" generated in the open password
dialog (wrong password) from an error "5408" generated in the modify
password dialog (cancel) and handle it accordingly?

Again my overall objective is to prevent read only files from being opened
programatically. So if there is a better way then please advise.

Thanks.

Sub ScratchMacro()
Dim oDoc As Word.Document
Dim i As Long
i = 1
Retry_Password:
On Error GoTo Err_Password
Set oDoc = Documents.Open("C:\Batch\Doc 1.doc")
If oDoc.ReadOnly Then
oDoc.Close wdDoNotSaveChanges
End If
On Error GoTo 0
Exit Sub
Err_ReEntry:
On Error Resume Next
oDoc.Close
If Err.Number <> 0 Then
ActiveDocument.Close wdDoNotSaveChanges
Err.Clear
End If
Exit Sub
Err_Password:
MsgBox Err.Number
'Wrong password.
If Err.Number = 5408 Then
If i < 3 Then
If MsgBox("You entered an incorrect password please try again.",
vbInformation + vbOKCancel, "Incorrect Password") = vbOK Then
i = i + 1
Resume Retry_Password
Else
MsgBox "File is password protected and cannot be opened withot a valid
password.", vbOKOnly, "Incorrect Password"
i = i + 1
Resume Retry_Password
End If
End If
MsgBox "Your third attempt failed. This file is password protected and
cannot be opened withot a valid password.", vbOKOnly, "Incorrect Password"
ElseIf Err.Number = 4198 Then
MsgBox "File is password protected and cannot be opened withot a valid
password.", vbOKOnly, "Incorrect Password"
ElseIf Err.Number = 5485 Then
MsgBox "File is password protected and could not be opened.", vbOKOnly,
"Incorrect Password"
Resume Err_ReEntry
End If
End Sub
 

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