Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Protect Documents and Parts...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
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.

NoteNote:

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.

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

  • Call the Protect method of the Microsoft.Office.Tools.Word..::.Document class.

    Visual Basic
    Me.Protect(Word.WdProtectionType.wdAllowOnlyReading, _
        False, String.Empty, False, False)
    
    
    C#
    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.

    Visual Basic
    Me.Protect(Word.WdProtectionType.wdAllowOnlyReading, _
        False, String.Empty, False, False)
    
    
    C#
    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.

    Visual Basic
    Bookmark1.Range.Editors.Add(Word.WdEditorType.wdEditorEveryone)
    
    
    C#
    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.

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

  • Call the Protect method of the Microsoft.Office.Interop.Word..::.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.

    Visual Basic
    Me.Application.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyReading, _
        False, String.Empty, False, False)
    
    
    C#
    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);
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker