From: ToddC on
the following code to override the protect button protects a document
properly but renders a "runtime error 5485: The password is incorrect" when
trying to unprotect. Seems simple enough, but I'm not sure why this is
happening.


Sub toolsprotectdocument()
DocPass = "Password"
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:=DocPass, noReset:=True,
Type:=wdAllowOnlyFormFields
Else
Dialogs(wdDialogToolsUnprotectDocument).Show
End If
End Sub

From: ToddC on
ok, I'll answer my own question for the sake of others....

While this approach did work in Word 2000, I have come to the conclusion
that it doesn't work in Word 2007. I have worked around this by:

1) creating a custom dialog called dlgUnprotectDocument. The custom dialog
was required because I wanted to keep the Password hidden. The dialog's code
is omitted here for simplicity's sake.

2) when OK in the dialog was clicked, the password was saved to a global var
gPassword.

3) when control was returned from the dialog, the activedocument.unprotect
method is used with gPassword.

This solution is not elegant, but it works.

Sub toolsprotect()
docpass = "Password"
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.ProtectPassword:=docpass, noreset:=True,
Type:=wdAllowOnlyFormFields
Else
dlgUnprotectDocument.Show
If Len(gPassword) = 0 Then Exit Sub
ActiveDocument.Unprotect Password:=gPassword
End If
End Sub

"ToddC" wrote:

> the following code to override the protect button protects a document
> properly but renders a "runtime error 5485: The password is incorrect" when
> trying to unprotect. Seems simple enough, but I'm not sure why this is
> happening.
>
>
> Sub toolsprotectdocument()
> DocPass = "Password"
> If ActiveDocument.ProtectionType = wdNoProtection Then
> ActiveDocument.Protect Password:=DocPass, noReset:=True,
> Type:=wdAllowOnlyFormFields
> Else
> Dialogs(wdDialogToolsUnprotectDocument).Show
> End If
> End Sub
>