Aracılığıyla paylaş


How to: Protect Documents and Parts of Documents

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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

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 Document Protector Sample.

If you are developing a document-level customization for Microsoft Office Word 2007, 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 Microsoft.Office.Tools.Word.Document class.

    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)
    
    object editorID = Word.WdEditorType.wdEditorEveryone;
    this.bookmark1.Range.Editors.Add(ref editorID);
    

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(WdProtectionType, Object, Object, Object, Object) 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)
    
    object noReset = false;
    object password = System.String.Empty;
    object useIRM = false;
    object enforceStyleLock = false;
    
    this.Application.ActiveDocument.Protect(
        Word.WdProtectionType.wdAllowOnlyReading,
        ref noReset, ref password, ref useIRM, ref enforceStyleLock);
    

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

Creating Office Solutions in Visual Studio