Protect Method

Microsoft Word Visual Basic

Show All Show All

Protect Method

Helps to protect the specified document from changes. When a document is protected, users can make only limited changes, such as adding annotations, making revisions, or completing a form.

Note  If the document is already protected when you use this method, an error occurs.

expression.Protect(Type, NoReset, Password, UseIRM, EnforceStyleLock)

expression    Required. An expression that returns a Document object.

Type   Required. The protection type for the specified document. WdProtectionType.

WdProtectionType can be one of these WdProtectionType constants.
wdAllowOnlyComments
wdAllowOnlyFormFields
wdAllowOnlyReading
wdAllowOnlyRevisions
wdNoProtection

NoReset   Optional Variant. False to reset form fields to their default values. True to retain the current form field values if the specified document is protected. If Type isn't wdAllowOnlyFormFields, the NoReset argument is ignored.

Password   Optional Variant. The password required to remove protection from the specified document. (See Remarks below.)

UseIRM    Optional Variant. Specifies whether to use Information Rights Management (IRM) when protecting the document from changes.

EnforceStyleLock    Optional Variant. Specifies whether formatting restrictions are enforced in a protected document.

Remarks

Security   Avoid using hard-coded passwords in your applications. If a password is required in a procedure, request the password from the user, store it in a variable, and then use the variable in your code. For recommended best practices on how to do this, see Security Notes for Microsoft Office Solution Developers.

Example

This example protects the active document for forms without resetting the contents of the form fields.

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

This example protects Monthly Report.doc so that only comments can be added to it. The password is required to unprotect the document.

    Set myDoc = Documents("Monthly Report.doc")
myDoc.Protect Type:=wdAllowOnlyComments, Password:=strPassword