Partilhar via


How to: Programmatically Protect Documents and Parts of Documents

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 application-level projects for Word 2013 and Word 2010. 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.

Protecting a Document That Is Part of a Document-Level Customization

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

  • 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);
    

Compiling 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.

Protecting a Document by Using an Application-Level Add-In

To protect a document by using an application-level add-in

  • 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

Tasks

How to: Permit Code to Run Behind Documents with Restricted Permissions

How to: Add Bookmark Controls to Word Documents

Concepts

Document Protection in Document-Level Solutions

Password Protection on Office Documents

Other Resources

Designing and Creating Office Solutions