Share via


How to: Programmatically protect documents and parts of documents

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can add protection to Microsoft Office Word documents to prevent users from making any edits to the document.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.

You can also mark certain areas of the document as exceptions so that specified users can edit only those areas of the document. For example, you might want to protect an entire document except for a particular bookmark. You can optionally add a password so that users cannot remove the document protection unless they know the password.

Note

The following example does not use password protection; however, you might want to consider using a password when adding document protection. For more information, see the Document Protector Sample at Office development samples and walkthroughs.

You can also use content controls to protect parts of documents. For more information, see How to: Protect parts of documents by using content controls.

Protect a document that is part of a document-level customization

To protect a document that is part of a document-level customization

  1. Call the Protect method of the ThisDocument class in your project.

    Me.Protect(Word.WdProtectionType.wdAllowOnlyReading, _
        False, String.Empty, False, False)
    
    object noReset = false;
    object password = System.String.Empty;
    object useIRM = false;
    object enforceStyleLock = false;
    
    this.Protect(Word.WdProtectionType.wdAllowOnlyReading, 
        ref noReset, ref password, ref useIRM, ref enforceStyleLock);
    

To exclude a bookmark control from document protection

  1. Protect the entire document using the Protect method.

    Me.Protect(Word.WdProtectionType.wdAllowOnlyReading, _
        False, String.Empty, False, False)
    
    object noReset = false;
    object password = System.String.Empty;
    object useIRM = false;
    object enforceStyleLock = false;
    
    this.Protect(Word.WdProtectionType.wdAllowOnlyReading, 
        ref noReset, ref password, ref useIRM, ref enforceStyleLock);
    
  2. Exclude Bookmark1 from the document protection.

    Bookmark1.Range.Editors.Add(Word.WdEditorType.wdEditorEveryone)
    
    this.bookmark1.Range.Editors.Add(Word.WdEditorType.wdEditorEveryone);
    

Compile the code

To use these code examples, run them from the ThisDocument class in your project. These code examples assume you have an existing Bookmark control named Bookmark1 on the document in which this code appears.

Protect a document by using a VSTO Add-in

To protect a document by using an application-level VSTO Add-in

  1. Call the Protect method of the Document that you want to protect.

    The following code example protects the active document. To use this code example, run it from the ThisAddIn class in your project.

    Me.Application.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyReading, _
        False, String.Empty, False, False)
    
    this.Application.ActiveDocument.Protect(
        Word.WdProtectionType.wdAllowOnlyReading,
        false, System.String.Empty, false, false);
    

See also