How to: Protect Parts of Documents by Using Content Controls

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

Applies to: The information in this topic applies to document-level projects and application-level projects for Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.

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.

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

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

    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()
    {
        // Use the following line of code in projects that target the .NET Framework 4.
        Microsoft.Office.Tools.Word.Document vstoDocument = 
            Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        // 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

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

    The following code example for a document-level project 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");
    }
    
    
    

    The following code example for an application-level project 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()
    {
        // Use the following line of code in projects that target the .NET Framework 4.
        Microsoft.Office.Tools.Word.Document vstoDocument =
            Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        // 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");
    }
    
    
    
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft