Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Protect Parts of Documents by Using Content Controls

Updated: July 2008

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 2007

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

When you protect part of a document, you prevent users from changing or deleting the content in that part of the document. There are several ways you can protect parts of a Microsoft Office Word 2007 document by using content controls:

  • You can protect a content control.

  • You can protect a part of a document that is not in a content control.

You can prevent users from editing or deleting a content control by setting properties of the control in a document-level project at design time or at run time.

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can also protect content controls that you add to a document at run time by using an application-level project. For more information, see How to: Add Content Controls to Word Documents.

To protect a content control at design time

  1. In the document that is hosted in the Visual Studio designer, select the content control that you want to protect.

  2. In the Properties window, set one or both of the following properties:

    • To prevent users from editing the control, set LockContents to True.

    • To prevent users from deleting the control, set LockContentControl to True.

  3. Click OK.

To protect a content control at run time in a document-level project

  • Set the LockContents property of the content control to true to prevent users from editing the control, and set the LockContentControl property to true to prevent users from deleting the control.

    The following code example demonstrates using the LockContents and LockContentControl properties of two different RichTextContentControl objects in a document-level project. To run this code, add the code to the ThisDocument class in your project, and call the AddProtectedContentControls method from the ThisDocument_Startup event handler.

    private Microsoft.Office.Tools.Word.RichTextContentControl deletableControl;
    private Microsoft.Office.Tools.Word.RichTextContentControl editableControl;
    
    private void AddProtectedContentControls()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = this.Paragraphs[1].Range;
    
        deletableControl = this.Controls.AddRichTextContentControl(range1,
            "deletableControl");
        deletableControl.PlaceholderText = "You can delete this control, " +
            "but you cannot edit it";
        deletableControl.LockContents = true;
    
        range1.InsertParagraphAfter();
        Word.Range range2 = this.Paragraphs[2].Range;
    
        editableControl = this.Controls.AddRichTextContentControl(range2,
            "editableControl");
        editableControl.PlaceholderText = "You can edit this control, " +
            "but you cannot delete it";
        editableControl.LockContentControl = true;
    }
    

To protect a content control at run time in an application-level project

  • Set the LockContents property of the content control to true to prevent users from editing the control, and set the LockContentControl property to true to prevent users from deleting the control.

    The following code example demonstrates using the LockContents and LockContentControl properties of two different RichTextContentControl objects in an application-level project. To run this code, add the code to the ThisAddIn class in your project, and call the AddProtectedContentControls method from the ThisAddIn_Startup event handler.

    private Microsoft.Office.Tools.Word.RichTextContentControl deletableControl;
    private Microsoft.Office.Tools.Word.RichTextContentControl editableControl;
    
    private void AddProtectedContentControls()
    {
        Microsoft.Office.Tools.Word.Document vstoDocument = 
            this.Application.ActiveDocument.GetVstoObject();
        vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = vstoDocument.Paragraphs[1].Range;
    
        deletableControl = vstoDocument.Controls.AddRichTextContentControl(range1,
            "deletableControl");
        deletableControl.PlaceholderText = "You can delete this control, " +
            "but you cannot edit it";
        deletableControl.LockContents = true;
    
        range1.InsertParagraphAfter();
        Word.Range range2 = vstoDocument.Paragraphs[2].Range;
    
        editableControl = vstoDocument.Controls.AddRichTextContentControl(range2,
            "editableControl");
        editableControl.PlaceholderText = "You can edit this control, " +
            "but you cannot delete it.";
        editableControl.LockContentControl = true;
    }
    

You can prevent users from changing an area of a document by putting the area in a GroupContentControl. This is useful in the following scenarios:

  • You want to protect an area that does not contain content controls.

  • You want to protect an area that already contains content controls, but the text or other items that you want to protect are not in the content controls.

NoteNote:

If you create a GroupContentControl that contains embedded content controls, the embedded content controls are not automatically protected. To prevent users from editing an embedded content control, use the LockContents property of the control.

To protect an area of a document at design time

  1. In the document that is hosted in the Visual Studio designer, select the area that you want to protect.

  2. On the Ribbon, click the Developer tab.

    NoteNote:

    If the Developer tab is not visible, you must first show it. For more information, see How to: Show the Developer Tab on the Ribbon.

  3. In the Controls group, click the Group drop-down button, and then click Group.

    A GroupContentControl that contains the protected region is automatically generated in the ThisDocument class in your project. A border that represents the group control is visible at design time, but there is no visible border at run time.

To protect an area of a document at run time in a document-level project

  • Programmatically select the area that you want to protect, and then call the AddGroupContentControl method to create a GroupContentControl.

    The following code example adds text to the first paragraph in the document, selects the first paragraph, and then instantiates a GroupContentControl. To run this code, add the code to the ThisDocument class in your project, and call the ProtectFirstParagraph method from the ThisDocument_Startup event handler.

    private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;
    
    private void ProtectFirstParagraph()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = this.Paragraphs[1].Range;
    
        range1.Text = "You cannot edit or change the formatting of text " +
            "in this sentence, because this sentence is in a GroupContentControl.";
        range1.Select();
        groupControl1 = this.Controls.AddGroupContentControl("groupControl1");
    }
    

To protect an area of a document at run time in an application-level project

  • Programmatically select the area that you want to protect, and then call the AddGroupContentControl method to create a GroupContentControl.

    The following code example adds text to the first paragraph in the active document, selects the first paragraph, and then instantiates a GroupContentControl. To run this code, add the code to the ThisAddIn class in your project, and call the ProtectFirstParagraph method from the ThisAddIn_Startup event handler.

    private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;
    
    private void ProtectFirstParagraph()
    {
        Microsoft.Office.Tools.Word.Document vstoDocument =
            this.Application.ActiveDocument.GetVstoObject();
        vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
    
        Word.Range range1 = vstoDocument.Paragraphs[1].Range;
        range1.Text = "You cannot edit or change the formatting of text " +
            "in this sentence, because this sentence is in a GroupContentControl.";
        range1.Select();
    
        groupControl1 = vstoDocument.Controls.AddGroupContentControl("groupControl1");
    }
    

Date

History

Reason

July 2008

Added procedures for application-level add-ins.

SP1 feature change.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft