O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Protection-Related Properties and Methods

The Document object has the following several properties related to document protection:

The Protect method and ProtectionType property

The Protect method protects the document from changes. When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form.

The syntax of the Protect method is:

DocumentObject.Protect(Type, NoReset, Password)

where the required Type parameter specifies the protection type for the document. It can be one of the constants in the following enum:

Enum WdProtectionType
   wdNoProtection = -1
   wdAllowOnlyRevisions = 0
   wdAllowOnlyComments = 1
   wdAllowOnlyFormFields = 2
End Enum

NoReset is related to any form fields on the document. For more on this, refer to the help documentation. Finally, the optional Password parameter can be set to a string password that subsequently will be required in order to unprotect the document.

Note that if a document is already protected, the Protect method will cause a runtime error, so the document should first be checked by looking at its ProtectionType property, which can assume any of the WdProtectionType constants described earlier. This is illustrated in the following code:

If ActiveDocument.ProtectionType = wdNoProtection Then
   ActiveDocument.Protect wdAllowOnlyComments,,"midnight"
End If
The UnProtect method

This method removes protection from the document. However, if the document is not currently protected, this method ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required