逐步解說:使用內容控制項建立範本

本逐步解說示範如何建立文件層級自訂,這個自訂會使用內容控制項在 Microsoft Office Word 範本中建立可重複使用的結構化內容。

**適用於:**本主題中的資訊適用於 Word 2007 和 Word 2010 的文件層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

Word 可以讓您建立可重複使用文件組件的集合,稱為「建置組塊」(Building Block)。 這個逐步解說顯示如何建立兩個資料表做為建置組塊。 每個資料表包含數個內容控制項,可以有不同的內容類型,例如純文字或日期。 其中一個資料表包含員工的資訊,另一個資料表則包含客戶回函。

從範本建立文件之後,您可以將任一個資料表加入至文件,方法是使用數個 BuildingBlockGalleryContentControl 物件,這些物件會顯示範本中可用的建置組塊。

這個逐步解說將說明下列工作:

  • 在設計階段建立包含 Word 範本中內容控制項的資料表。

  • 透過程序化的流程填入 (Populate) 下拉式方塊內容控制項和下拉式清單內容控制項。

  • 防止使用者編輯指定的資料表。

  • 將資料表加入至範本的建置組塊集合。

  • 建立會顯示範本中可用建置組塊的內容控制項。

注意事項注意事項

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱 Visual Studio 設定

必要條件

您需要下列元件才能完成此逐步解說:

-

包含 Microsoft Office 開發者工具的 Visual Studio 2010 版本。 如需詳細資訊,請參閱[設定電腦以開發 Office 方案](bb398242\(v=vs.100\).md)。
  • Microsoft Office Word 2007 或 Word 2010。

建立新的 Word 範本專案

建立 Word 範本,讓使用者可以輕鬆建立自己的複本。

若要建立新的 Word 範本專案

  • 建立名為 MyBuildingBlockTemplate 的 Word 範本專案。 在精靈中,於方案中建立新文件。 如需詳細資訊,請參閱 HOW TO:在 Visual Studio 中建立 Office 專案

    Visual Studio 會在設計工具中開啟新的 Word 範本,並且將 [MyBuildingBlockTemplate] 專案加入至 [方案總管]。

建立員工資料表

建立含有四種不同內容控制項的資料表,使用者可以在這個資料表中輸入員工資訊。

若要建立員工資料表

  1. 在 Visual Studio 設計工具所裝載 (Host) 的 Word 範本中,按一下功能區上的 [插入] 索引標籤。

  2. 按一下 [資料表] 群組中的 [資料表],並插入具有 2 欄 4 列的資料表。

  3. 請在第一欄中輸入文字,使該欄看起來像下面這一個資料行:

    員工姓名

    雇用日期

    標題

    圖片

  4. 按一下第二欄中的第一個儲存格 ([員工姓名] 旁邊)。

  5. 按一下 [功能區] 上的 [開發人員] 索引標籤。

    注意事項注意事項

    如果 [開發人員] 索引標籤沒有顯示,您必須先使其顯示。 如需詳細資訊,請參閱 HOW TO:在功能區顯示開發人員索引標籤

  6. 按一下 [控制項] 群組中的 [文字] 按鈕 PlainTextContentControl,將 PlainTextContentControl 加入至第一個儲存格。

  7. 按一下第二欄中的第二個儲存格 ([雇用日期] 旁邊)。

  8. 按一下 [控制項] 群組中的 [日期選擇器] 按鈕 DatePickerContentControl,將 DatePickerContentControl 加入至第二個儲存格。

  9. 按一下第二欄中的第三個儲存格 ([標題] 旁邊)。

  10. 按一下 [控制項] 群組中的 [下拉式方塊] 按鈕 ComboBoxContentControl,將 ComboBoxContentControl 加入至第三個儲存格。

  11. 按一下第二欄中的最後一個儲存格 ([圖片] 旁邊)。

  12. 按一下 [控制項] 群組中的 [圖片內容控制項] 按鈕 PictureContentControl,將 PictureContentControl 加入至最後一個儲存格。

建立客戶回函資料表

建立含有三種不同內容控制項類型的資料表,使用者可以在這個資料表中輸入客戶回函資訊。

若要建立客戶回函資料表

  1. 在 Word 範本中,按一下您稍早加入之員工資料表後面一行,然後按下 ENTER 鍵加入新段落。

  2. 按一下功能區上的 [插入] 索引標籤。

  3. 按一下 [資料表] 群組中的 [資料表],並插入具有 2 欄 3 列的資料表。

  4. 請在第一欄中輸入文字,使該欄看起來像下面這一個資料行:

    客戶名稱

    滿意度

    註解

  5. 按一下第二欄的第一個儲存格 ([客戶名稱] 旁邊)。

  6. 按一下 [功能區] 上的 [開發人員] 索引標籤。

  7. 按一下 [控制項] 群組中的 [文字] 按鈕 PlainTextContentControl,將 PlainTextContentControl 加入至第一個儲存格。

  8. 按一下第二欄的第二個儲存格 ([滿意度] 旁邊)。

  9. 按一下 [控制項] 群組中的 [下拉式清單] 按鈕 DropDownListContentControl,將 DropDownListContentControl 加入至第二個儲存格。

  10. 按一下第二欄的最後一個儲存格 ([註解] 旁邊)。

  11. 按一下 [控制項] 群組中的 [Rich Text 格式] 按鈕 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. 關閉範本。

後續步驟

您可以透過下列主題,進一步了解使用內容控制項的方式:

請參閱

工作

HOW TO:將內容控制項加入至 Word 文件

HOW TO:使用內容控制項保護文件的部分

概念

使用擴充物件自動化 Word

內容控制項

主項目和主控制項概觀

主項目和主控制項的程式設計限制

在執行階段將控制項加入至 Office 文件

主控制項的 Helper 方法