Protect Form code

H

hermithead

Hi, How do you make the following code Protect only? This code will
protect an unprotected form AND unprotect a protected form. I just
need it to Protect.


Sub ProtectForm()

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
Else
ActiveDocument.Unprotect Password:=""
End If

End Sub
 
G

Graham Mayor

ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

hermithead

The code works fine

Sub ProtectForm()

ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""

End Sub


but if the macrobutton is clicked again then the following error
displays:
Run time error 4605 This Project method or property is not available
because the document is already protected.
 
G

Graham Mayor

Strictly speaking, if the document is already protected it doesn't need
protecting but

Sub ProtectMyForm()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=""
Else
MsgBox "Document is already protected"
End If
End Sub

will do the trick. Leave out the
Else
MsgBox "Document is already protected"

if you prefer. NOTE that your macro name ProtectForm is the name of the
in-built function for protecting forms and will interfere with the normal
use of that function.
 

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