演练:使用内容控件创建模板

本演练演示如何创建一个文档级自定义项,该自定义项使用内容控件在 Microsoft Office Word 模板中创建可重用的结构化内容。

**适用于:**本主题中的信息适用于 Word 2007 和 Word 2010 的文档级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

通过使用 Word 可以创建名为“生成块”的可重用文档部分的集合。 此演练演示如何将两个表创建为生成块。 每个表包含一些内容控件,这些内容控件可以容纳不同类型的内容,如纯文本或日期。 一个表包含雇员的信息,而另一个表则包含客户反馈。

从模板中创建文档后,通过使用一些显示模板中的可用生成块的 BuildingBlockGalleryContentControl 对象,可以向文档添加其中任何一个表。

本演练阐释了以下任务:

  • 设计时在 Word 模板中创建包含内容控件的表。

  • 以编程方式填充组合框内容控件和下拉列表内容控件。

  • 阻止用户编辑指定表。

  • 向模板的生成块集合添加表。

  • 创建显示模板中可用生成块的内容控件。

提示

对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您所使用的 Visual Studio 版本和您所使用的设置。有关更多信息,请参见 Visual Studio 设置

系统必备

您需要以下组件来完成本演练:

-

Visual Studio 2010 的一个版本,其中包含 Microsoft Office 开发工具。有关更多信息,请参见[将计算机配置为开发 Office 解决方案](bb398242\(v=vs.100\).md)。
  • Microsoft Office Word 2007 或 Word 2010。

创建新 Word 模板项目

创建 Word 模板以便用户可以轻松地创建自己的副本。

创建新的 Word 模板项目

  • 创建一个名为“MyBuildingBlockTemplate”的 Word 模板项目。 利用向导在解决方案中创建新文档。 有关更多信息,请参见如何:在 Visual Studio 中创建 Office 项目

    Visual Studio 在设计器中打开新 Word 模板并向**“解决方案资源管理器”添加“我的生成块模板”**项目。

创建雇员表

创建一个表,此表包含四种不同类型的内容控件,用户可以在这些内容控件中输入有关雇员的信息。

创建雇员表

  1. 在 Visual Studio 设计器承载的 Word 模板中,单击功能区上的**“插入”**选项卡。

  2. 在**“表”组中,单击“表”**,并插入一个包含二列四行的表。

  3. 在第一列中键入文本,以使该列与下面的列相似:

    雇员姓名

    雇佣日期

    标题

    图片

  4. 单击第二列的第一个单元格(在**“雇员姓名”**旁边)。

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

    提示

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

  6. 在**“控件”组中,单击“文本”**按钮 PlainTextContentControl,将 PlainTextContentControl 添加到第一个单元格中。

  7. 单击第二列的第二个单元格(在**“雇佣日期”**旁边)。

  8. 在**“控件”组中,单击“日期选择器”**按钮 DatePickerContentControl,将 DatePickerContentControl 添加到第二个单元格中。

  9. 单击第二列的第三个单元格(在**“职称”**旁边)。

  10. 在**“控件”组中,单击“组合框”**按钮 ComboBoxContentControl 以将 ComboBoxContentControl 添加到第三个单元格中。

  11. 单击第二列的最后一个单元格(在**“图片”**旁边)。

  12. 在**“控件”组中,单击“图片内容控件”**按钮 PictureContentControl 以将 PictureContentControl 添加到最后一个单元格中。

创建客户反馈表

创建一个表,此表包含三种不同类型的内容控件,用户可以在这些内容控件中输入客户反馈信息。

创建客户反馈表

  1. 在 Word 模板中,单击之前添加的雇员表后面的行,并按 Enter 以添加新段落。

  2. 在功能区上,单击**“插入”**选项卡。

  3. 在**“表”组中,单击“表”**,并插入一个包含二列三行的表。

  4. 在第一列中键入文本,以使该列与下面的列相似:

    客户名称

    满意度评价

    注释

  5. 单击第二列的第一个单元格(在**“客户名称”**旁边)。

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

  7. 在**“控件”组中,单击“文本”**按钮 PlainTextContentControl,将 PlainTextContentControl 添加到第一个单元格中。

  8. 单击第二列的第二个单元格(在**“满意度评价”**旁边)。

  9. 在**“控件”组中,单击“下拉列表”**按钮 DropDownListContentControl 以将 DropDownListContentControl 添加到第二个单元格中。

  10. 单击第二列的最后一个单元格(在**“注释”**旁边)。

  11. 在**“控件”组中,单击“多格式文本”**按钮 RichTextContentControl 以将 RichTextContentControl 添加到最后一个单元格中。

以编程方式填充组合框和下拉列表

通过使用 Visual Studio 中的**“属性”**窗口可以在设计时初始化内容控件。 也可以在运行时初始化内容控件,这样可以动态设置它们的初始状态。 对于此演练,请使用代码在运行时在 ComboBoxContentControlDropDownListContentControl 中填充项以便可以查看这些对象的工作方式。

以编程方式修改内容控件的 UI

  1. 在**“解决方案资源管理器”中右击 ThisDocument.cs 或 ThisDocument.vb,然后单击“查看代码”**。

  2. 向 ThisDocument 类中添加下面的代码。 此代码声明稍后将在此演练中使用的一些对象。

    Private GroupControl1 As Microsoft.Office.Tools.Word.GroupContentControl
    Private BuildingBlockControl1 As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
    Private BuildingBlockControl2 As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
    
    private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;
    private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl1;
    private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl2;
    
  3. 将以下代码添加到 ThisDocument 类的 ThisDocument_Startup 方法。 此代码向表中的 ComboBoxContentControlDropDownListContentControl 添加项,并设置占位符文本,该文本在用户编辑这些控件之前会显示在每个控件中。

    ComboBoxContentControl1.PlaceholderText = "Choose a title, or enter your own"
    ComboBoxContentControl1.DropDownListEntries.Add("Engineer", "Engineer", 0)
    ComboBoxContentControl1.DropDownListEntries.Add("Designer", "Designer", 1)
    ComboBoxContentControl1.DropDownListEntries.Add("Manager", "Manager", 2)
    
    DropDownListContentControl1.PlaceholderText = _
        "Choose a rating (1 lowest, 3 highest)"
    DropDownListContentControl1.DropDownListEntries.Add("1", "1", 0)
    DropDownListContentControl1.DropDownListEntries.Add("2", "2", 1)
    DropDownListContentControl1.DropDownListEntries.Add("3", "3", 2)
    
    comboBoxContentControl1.PlaceholderText = "Choose a title, or enter your own";
    comboBoxContentControl1.DropDownListEntries.Add("Engineer", "Engineer", 0);
    comboBoxContentControl1.DropDownListEntries.Add("Designer", "Designer", 1);
    comboBoxContentControl1.DropDownListEntries.Add("Manager", "Manager", 2);
    
    dropDownListContentControl1.PlaceholderText =
        "Choose a rating (1 lowest, 3 highest)";
    dropDownListContentControl1.DropDownListEntries.Add("1", "1", 0);
    dropDownListContentControl1.DropDownListEntries.Add("2", "2", 1);
    dropDownListContentControl1.DropDownListEntries.Add("3", "3", 2);
    

阻止用户编辑雇员表

使用之前声明的 GroupContentControl 对象来保护雇员表。 保护雇员表后,用户仍可以在此表中编辑内容控件。 但是,无法编辑第一列中的文本或以其他方式修改表,如添加或删除行和列。 有关如何使用 GroupContentControl 保护部分文档的更多信息,请参见内容控件

阻止用户编辑雇员表

  • 向 ThisDocument 类的 ThisDocument_Startup 方法添加下列代码,所添加的代码在上一步骤中添加的代码之后。 此代码通过将表放入之前声明的 GroupContentControl 对象内来阻止用户编辑雇员表。

    Me.Tables(1).Select()
    GroupControl1 = Me.Controls.AddGroupContentControl("groupControl1")
    
    this.Tables[1].Range.Select();
    groupControl1 = this.Controls.AddGroupContentControl("groupControl1");
    

向生成块集合添加表

将表添加到模板中的文档生成块集合中,以便用户可以将已创建的表插入文档中。 有关文档生成块的更多信息,请参见内容控件

将表添加到模板的生成块中

  1. 向 ThisDocument 类的 ThisDocument_Startup 方法添加下列代码,所添加的代码在上一步骤中添加的代码之后。 此代码会将包含表的新生成块添加到 Microsoft.Office.Interop.Word.BuildingBlockEntries 集合中,该集合包含模板中的所有可重用生成块。 该新生成块是在名为 Employee and Customer Information 的新类别中定义的,并分配有 Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeCustom1 生成块类型。

    Dim template1 As Word.Template = TryCast(Me.AttachedTemplate, Word.Template)
    If template1 IsNot Nothing Then
    
        template1.BuildingBlockEntries.Add("Employee Table", _
            Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information", _
            Me.Tables(1).Range, InsertOptions:=Word.WdDocPartInsertOptions.wdInsertContent)
        template1.BuildingBlockEntries.Add("Customer Table", _
            Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information", _
            Me.Tables(2).Range, InsertOptions:=Word.WdDocPartInsertOptions.wdInsertContent)
    End If
    
    Word.Template template1 = this.AttachedTemplate as Word.Template;
    
    if (template1 != null)
    {
        object description = null;
        template1.BuildingBlockEntries.Add("Employee Table",
            Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information",
            this.Tables[1].Range, ref description, Word.WdDocPartInsertOptions.wdInsertContent);
        template1.BuildingBlockEntries.Add("Customer Table",
            Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information",
            this.Tables[2].Range, ref description, Word.WdDocPartInsertOptions.wdInsertContent);
    }
    
  2. 向 ThisDocument 类的 ThisDocument_Startup 方法添加下列代码,所添加的代码在上一步骤中添加的代码之后。 此代码将从模板中删除表。 您不再需要表,因为已经将这些表添加到模板的可重用生成块的库中。 首先,代码使文档处于设计模式,以便可以删除受保护的雇员表。

    If Me.FormsDesign = False Then
        Me.ToggleFormsDesign()
    End If
    Me.Tables(2).Delete()
    Me.Tables(1).Delete()
    Me.ToggleFormsDesign()
    
    if (!this.FormsDesign)
    {
        this.ToggleFormsDesign();
    }
    this.Tables[2].Delete();
    this.Tables[1].Delete();
    this.ToggleFormsDesign();
    

创建一个显示生成块的内容控件

创建一个内容控件,通过此内容控件可以访问之前创建的生成块(即,表)。 用户可以单击此控件以向文档添加表。

创建一个显示生成块的内容控件

  • 向 ThisDocument 类的 ThisDocument_Startup 方法添加下列代码,所添加的代码在上一步骤中添加的代码之后。 此代码初始化之前声明的 BuildingBlockGalleryContentControl 对象。 BuildingBlockGalleryContentControl 显示了在 Employee and Customer Information 类别中定义的具有 Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeCustom1 生成块类型的所有生成块。

    BuildingBlockControl1 = Me.Controls.AddBuildingBlockGalleryContentControl( _
        Me.Paragraphs(1).Range, "buildingBlockControl1")
    BuildingBlockControl1.BuildingBlockCategory = "Employee and Customer Information"
    BuildingBlockControl1.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1
    BuildingBlockControl1.PlaceholderText = "Choose your first building block"
    
    BuildingBlockControl2 = Me.Controls.AddBuildingBlockGalleryContentControl( _
        Me.Paragraphs(2).Range, "buildingBlockControl2")
    BuildingBlockControl2.BuildingBlockCategory = "Employee and Customer Information"
    BuildingBlockControl2.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1
    BuildingBlockControl2.PlaceholderText = "Choose your second building block"
    
    buildingBlockControl1 = this.Controls.AddBuildingBlockGalleryContentControl(
        this.Paragraphs[1].Range, "buildingBlockControl1");
    buildingBlockControl1.BuildingBlockCategory = "Employee and Customer Information";
    buildingBlockControl1.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1;
    buildingBlockControl1.PlaceholderText = "Choose your first building block";
    
    buildingBlockControl2 = this.Controls.AddBuildingBlockGalleryContentControl(
        this.Paragraphs[2].Range, "buildingBlockControl2");
    buildingBlockControl2.BuildingBlockCategory = "Employee and Customer Information";
    buildingBlockControl2.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1;
    buildingBlockControl2.PlaceholderText = "Choose your second building block";
    

测试项目

用户可以单击文档中的生成块库控件以插入雇员表或客户反馈表。 用户可以在这两个表的内容控件中键入或选择响应。 用户可以修改客户反馈表的其他部分,但应该不能修改雇员表的其他部分。

测试雇员表

  1. 按 F5 运行项目。

  2. 单击 Choose your first building block 以显示第一个生成块库的内容控件。

  3. 单击控件中**“自定义库 1”标题旁边的下拉箭头,并选择“雇员表”**。

  4. 在“雇员姓名”单元格右侧的单元格中单击,并键入姓名。

    验证是否只可以向此单元格添加文本。 PlainTextContentControl 仅允许用户添加文本,而不允许添加其他类型的内容(如图片或表)。

  5. 在“雇佣日期”单元格右侧的单元格中单击,并在日期选择器中选择日期。

  6. 在“职称”单元格右侧的单元格中单击,并在组合框中选择一个职务。

    (可选)键入未在列表中的职务的名称。 之所以可以这样,是因为 ComboBoxContentControl 使用户可以从项列表中选择或键入用户自己的项。

  7. 单击“图片”单元格右侧单元格内的图标并浏览到某个图像以显示此图像。

  8. 尝试将行或列添加到表中,并尝试从表中删除行和列。 验证是否不能修改该表。 GroupContentControl 阻止您进行任何修改。

测试客户反馈表

  1. 单击 Choose your second building block 以显示第二个生成块库的内容控件。

  2. 单击控件中**“自定义库 1”标题旁边的下拉箭头,并选择“客户表”**。

  3. 在“客户姓名”单元格右侧的单元格中单击,并键入姓名。

  4. 在“满意度评价”单元格右侧的单元格中单击,并选择一个可用选项。

    验证是否不能键入自己的项。 DropDownListContentControl 仅允许用户从项列表中进行选择。

  5. 在“注释”单元格右侧的单元格中单击,并键入一些注释。

    (可选)添加一些非文本内容,例如图片或嵌入的表。 之所以可以这样,是因为 RichTextContentControl 使用户可以添加非文本内容。

  6. 验证是否可以将行或列添加到表中,以及是否可以从表中删除行和列。 之所以可以这样,是因为并未通过将表放入 GroupContentControl 中来保护表。

  7. 关闭模板。

后续步骤

您可以从以下主题中了解有关如何使用内容控件的更多信息:

请参见

任务

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

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

概念

使用扩展对象实现 Word 自动化

内容控件

宿主项和宿主控件概述

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

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

宿主控件的帮助器方法