Unprotect Doc w/word 2007 VBA

A

alfred

What am I missing. Using word 2007 VBA code:

When Using code "ActiveDocument.Unprotect" on a protected document I get an
error when I was expecting the "Enter Password" Dialog Box.

I even recorded macros to protect then unprotect the document. All worked
well with the keystrokes when recording but when I stepped thru the code I
got the error message instead of the dialog box.
 
J

Jay Freedman

What am I missing. Using word 2007 VBA code:

When Using code "ActiveDocument.Unprotect" on a protected document I get an
error when I was expecting the "Enter Password" Dialog Box.

I even recorded macros to protect then unprotect the document. All worked
well with the keystrokes when recording but when I stepped thru the code I
got the error message instead of the dialog box.

All versions of Word have always worked this way. The Unprotect method takes an
optional parameter, Password, and if you omit the parameter then VBA assigns the
empty string to it. If the protection password isn't empty/nonexistent, then you
get an error -- it will *not* prompt you.

Also be aware that things don't always work the same way in VBA as they do from
the keyboard, and the recorder isn't able to take that into account. This is one
of those cases; AutoCorrect completion is another.

You can take either of two approaches if you know the protection is passworded.
One is to include the password in the code:

Const pwd = "foo"

With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=pwd
End If

' do something in the document

.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=pwd
End With

Then you'll want to password-protect the code so it isn't a simple matter to
open the VBA editor and look at the form password (although that isn't absolute
protection either).

The second approach is to prompt the user for the password, which is necessary
if the password isn't always the same.

Dim pwd As String

pwd = InputBox("Enter form password:")
If Len(pwd) = 0 Then Exit Sub

On Error Resume Next
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=pwd
If Err.Number <> 0 Then
MsgBox "Password is incorrect."
Exit Sub
End If
End If

' do something in the document

.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=pwd
End With

If you don't know whether the form will have a password, then you can put the
InputBox prompt in an error handler:

Dim pwd As String
pwd = ""

ResumeHere:
On Error GoTo ErrHdl

With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=pwd
End If

' do something in the document
MsgBox "Unprotected"

.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=pwd
End With
Exit Sub

ErrHdl:
pwd = InputBox("Form requires a password:")
If Len(pwd) = 0 Then Exit Sub
Resume ResumeHere
 

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