Share via


DocumentBase.Protect(WdProtectionType, Object, Object, Object, Object) Method

Definition

Applies one of several types of protection to the document.

public void Protect (Microsoft.Office.Interop.Word.WdProtectionType type, ref object noReset, ref object password, ref object useIRM, ref object enforceStyleLock);
member this.Protect : Microsoft.Office.Interop.Word.WdProtectionType * obj * obj * obj * obj -> unit
Public Sub Protect (type As WdProtectionType, Optional ByRef noReset As Object, Optional ByRef password As Object, Optional ByRef useIRM As Object, Optional ByRef enforceStyleLock As Object)

Parameters

type
WdProtectionType

The protection type for the specified document. WdProtectionType.

noReset
Object

false to reset form fields to their default values. true to retain the current form field values if the document is protected. If Type is not wdAllowOnlyFormFields, the NoReset argument is ignored.

password
Object

The password required to remove protection from the document. (See Remarks below.)

useIRM
Object

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

enforceStyleLock
Object

Specifies whether formatting restrictions are enforced in a protected document.

Examples

The following code example uses the Protect method to allow users to add only comments to the document. To use this example, run it from the ThisDocument class in a document-level project.

private void DocumentProtect(ref object securelyStoredPassword)
{
    this.Protect(Word.WdProtectionType.wdAllowOnlyComments,
        ref missing, ref securelyStoredPassword, ref missing, 
        ref missing);
}
Private Sub DocumentProtect(ByRef securelyStoredPassword As Object)
    Me.Protect(Word.WdProtectionType.wdAllowOnlyComments, _
        Password:=securelyStoredPassword)
End Sub

Remarks

When a document is protected, users can make only limited changes, such as adding annotations, making revisions, or completing a form. If the document is already protected when you use this method, an exception is thrown.

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.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Applies to