如何:使用内容控件保护文档的某些部分

更新: 2008 年 7 月

适用于

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

  • 应用程序级项目

Microsoft Office 版本

  • Word 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

如果保护文档的某个部分,则将阻止用户更改或删除文档中该部分的内容。可以通过以下方式用内容控件来保护 Microsoft Office Word 2007 文档的某些部分:

  • 可以保护内容控件。

  • 可以保护不在内容控件中的文档的某个部分。

保护内容控件

可以在设计时或运行时通过在文档级项目设置控件的属性,来阻止用户编辑或删除内容控件。

从 Visual Studio 2008 Service Pack 1 (SP1) 开始,您还可以通过使用应用程序级项目,在运行时保护您添加到文档的内容控件。有关更多信息,请参见如何:向 Word 文档添加内容控件

在设计时保护内容控件

  1. 在 Visual Studio 设计器承载的文档中,选择想要保护的内容控件。

  2. 在“属性”窗口中,设置下列属性中的一个或两个属性:

    • 若要阻止用户编辑控件,请将“LockContents”设置为 True。

    • 若要阻止用户删除控件,请将“LockContentControl”设置为 True。

  3. 单击“确定”。

在运行时在文档级项目中保护内容控件

  • 将内容控件的 LockContents 属性设置为 true 可以阻止用户编辑该控件,将 LockContentControl 属性设置为 true 可以阻止用户删除该控件。

    下面的代码示例演示了如何在文档级项目中使用两个不同 RichTextContentControl 对象的 LockContentsLockContentControl 属性。若要运行此代码,请将代码添加到项目的 ThisDocument 类中,并从 ThisDocument_Startup 事件处理程序中调用 AddProtectedContentControls 方法。

    Dim deletableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    Dim editableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddProtectedContentControls()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        Dim range1 As Word.Range = Me.Paragraphs(1).Range
    
        deletableControl = Me.Controls.AddRichTextContentControl(range1, _
            "deletableControl")
        deletableControl.PlaceholderText = "You can delete this control, " & _
            "but you cannot edit it"
        deletableControl.LockContents = True
    
        range1.InsertParagraphAfter()
        Dim range2 As Word.Range = Me.Paragraphs(2).Range
    
        editableControl = Me.Controls.AddRichTextContentControl(range2, _
            "editableControl")
        editableControl.PlaceholderText = "You can edit this control, " & _
            "but you cannot delete it"
        editableControl.LockContentControl = True
    End Sub
    
    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;
    }
    

在运行时在应用程序级项目中保护内容控件

  • 将内容控件的 LockContents 属性设置为 true 可以阻止用户编辑该控件,将 LockContentControl 属性设置为 true 可以阻止用户删除该控件。

    下面的代码示例演示了如何在应用程序级项目中使用两个不同 RichTextContentControl 对象的 LockContentsLockContentControl 属性。若要运行此代码,请将代码添加到项目的 ThisAddIn 类中,并从 ThisAddIn_Startup 事件处理程序中调用 AddProtectedContentControls 方法。

    Dim deletableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    Dim editableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddProtectedContentControls()
        Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
            Me.Application.ActiveDocument.GetVstoObject()
        vstoDocument.Paragraphs(1).Range.InsertParagraphBefore()
        Dim range1 As Word.Range = 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()
        Dim range2 As Word.Range = 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
    End Sub
    
    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;
    }
    

保护不在内容控件中的文档的某个部分

通过将文档区域放置在 GroupContentControl 中,可以阻止用户更改该区域。此方法可用于以下情况:

  • 想要保护不包含内容控件的区域。

  • 想要保护已经包含内容控件的区域,但想要保护的文本或其他项不在内容控件中。

Bb386181.alert_note(zh-cn,VS.90).gif说明:

如果创建一个包含嵌入式内容控件的 GroupContentControl,则不会自动保护这些嵌入式内容控件。若要阻止用户编辑嵌入式内容控件,请使用该控件的 LockContents 属性。

在设计时保护文档区域

  1. 在 Visual Studio 设计器承载的文档中,选择想要保护的区域。

  2. 在功能区上,单击“开发人员”选项卡。

    Bb386181.alert_note(zh-cn,VS.90).gif说明:

    如果看不到“开发人员”选项卡,则必须首先显示该选项卡。有关更多信息,请参见如何:在功能区上显示“开发人员”选项卡

  3. 在“控件”组中,单击“组”下拉按钮,然后单击“组”。

    在项目的 ThisDocument 类中将自动生成包含受保护区域的 GroupContentControl。在设计时可以看见一个表示组控件的边框,但在运行时没有可见边框。

在运行时在文档级项目中保护文档区域

  • 以编程方式选择想要保护的区域,然后调用 AddGroupContentControl 方法以创建 GroupContentControl

    下面的代码示例将文本添加到文档的第一段,并选择第一段,然后实例化 GroupContentControl。若要运行此代码,请将代码添加到项目的 ThisDocument 类中,并从 ThisDocument_Startup 事件处理程序中调用 ProtectFirstParagraph 方法。

    Dim groupControl1 As Microsoft.Office.Tools.Word.GroupContentControl
    
    Private Sub ProtectFirstParagraph()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        Dim range1 As Word.Range = Me.Paragraphs(1).Range
        range1.Text = "You cannot edit or change the formatting of text " & _
                "in this paragraph, because this paragraph is in a GroupContentControl."
        range1.Select()
    
        groupControl1 = Me.Controls.AddGroupContentControl("groupControl1")
    End Sub
    
    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");
    }
    

在运行时在应用程序级项目中保护文档区域

  • 以编程方式选择想要保护的区域,然后调用 AddGroupContentControl 方法以创建 GroupContentControl

    下面的代码示例将文本添加到活动文档的第一段,并选择第一段,然后实例化一个 GroupContentControl。若要运行此代码,请将代码添加到项目的 ThisAddIn 类中,并从 ThisAddIn_Startup 事件处理程序中调用 ProtectFirstParagraph 方法。

    Dim groupControl1 As Microsoft.Office.Tools.Word.GroupContentControl
    
    Private Sub ProtectFirstParagraph()
        Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
            Me.Application.ActiveDocument.GetVstoObject()
        vstoDocument.Paragraphs(1).Range.InsertParagraphBefore()
    
        Dim range1 As Word.Range = vstoDocument.Paragraphs(1).Range
        range1.Text = "You cannot edit or change the formatting of text " & _
                "in this paragraph, because this paragraph is in a GroupContentControl."
        range1.Select()
    
        groupControl1 = vstoDocument.Controls.AddGroupContentControl("groupControl1")
    End Sub
    
    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");
    }
    

请参见

任务

如何:向 Word 文档添加内容控件

概念

内容控件

宿主项和宿主控件概述

宿主项和宿主控件的编程限制

在运行时向 Office 文档添加控件

宿主控件的帮助器方法

其他资源

Word 宿主控件

修订记录

日期

修订历史记录

原因

2008 年 7 月

增加了针对应用程序级外接程序的过程。

SP1 功能更改。