共用方式為


ControlCollection.AddBuildingBlockGalleryContentControl 方法 (ContentControl, String)

將新的 BuildingBlockGalleryContentControl 加入集合中。新的控制項是以文件中已有的原生內容控制項為基礎。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)

語法

'宣告
Function AddBuildingBlockGalleryContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As BuildingBlockGalleryContentControl
BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl(
    ContentControl contentControl,
    string name
)

參數

傳回值

型別:Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
加入至文件中的 BuildingBlockGalleryContentControl

例外狀況

例外狀況 條件
ArgumentNullException

contentControl 為 nullnull 參考 (即 Visual Basic 中的 Nothing)。

-或-

name 是 nullnull 參考 (即 Visual Basic 中的 Nothing),或長度為 0。

ControlNameAlreadyExistsException

ControlCollection 中已有相同名稱的控制項。

ArgumentException

contentControl 不是建置組塊陳列庫 (即 contentControl 的 Type 屬性,其值不是 Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlBuildingBlockGallery)。

備註

使用這個方法加入新的 BuildingBlockGalleryContentControl,此控制項是以文件中的原生內容控制項為基礎。當您在執行階段建立 BuildingBlockGalleryContentControl,而且希望下次開啟文件時再建立相同的控制項時,這個控制項很好用。如需詳細資訊,請參閱在執行階段將控制項加入至 Office 文件

範例

下列程式碼範例會為已經在文件中的每個原生建置組塊陳列庫建立新的 BuildingBlockGalleryContentControl

這是示範文件層級自訂的版本。若要使用這段程式碼,請將它貼到專案的 ThisDocument 類別中,並從 ThisDocument_Startup 方法呼叫 CreateBuildingBlockControlsFromNativeControls 方法。

Private buildingBlockControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl)

Private Sub CreateBuildingBlockGalleryControlsFromNativeControls()
    If Me.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In Me.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlBuildingBlockGallery Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl = _
                Me.Controls.AddBuildingBlockGalleryContentControl(nativeControl, _
                "VSTOBuildingBlockGalleryContentControl" + count.ToString())
            buildingBlockControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
   <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl> buildingBlockControls;

private void CreateBuildingBlockControlsFromNativeControls()
{
    if (this.ContentControls.Count <= 0)
        return;

    buildingBlockControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
        {
            count++;
            Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl tempControl =
                this.Controls.AddBuildingBlockGalleryContentControl(nativeControl,
                "VSTOBuildingBlockContentControl" + count.ToString());
            buildingBlockControls.Add(tempControl);
        }
    }
}

以 .NET Framework 4 或 .NET Framework 4.5的這個版本是應用程式層級增益集。若要使用這段程式碼,請將它貼到增益集專案的 ThisAddIn 類別中,並從 ThisAddIn_Startup 方法呼叫 CreateBuildingBlockControlsFromNativeControls 方法。

Private buildingBlockControls As New System.Collections.Generic.List _
        (Of BuildingBlockGalleryContentControl)

Private Sub CreateBuildingBlockGalleryControlsFromNativeControls()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If vstoDoc.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In vstoDoc.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlBuildingBlockGallery Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl = _
                vstoDoc.Controls.AddBuildingBlockGalleryContentControl(nativeControl, _
                "VSTOBuildingBlockGalleryContentControl" + count.ToString())
            buildingBlockControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
   <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl> buildingBlockControls;

private void CreateBuildingBlockControlsFromNativeControls()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (vstoDoc.ContentControls.Count <= 0)
    {
        System.Windows.Forms.MessageBox.Show("No content controls found in document.");                    
        return;
    }

    buildingBlockControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
        {
            count++;
            Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl tempControl =
                vstoDoc.Controls.AddBuildingBlockGalleryContentControl(nativeControl,
                "VSTOBuildingBlockContentControl" + count.ToString());
            buildingBlockControls.Add(tempControl);
        }
    }
}

下列程式碼範例會為使用者加入至文件中的每個原生建置組塊陳列庫建立新的 BuildingBlockGalleryContentControl

這是示範文件層級自訂的版本。若要使用這段程式碼,請將它貼到專案的 ThisDocument 類別中。若為 C#,您還必須將 ThisDocument_BuildingBlockContentControlAfterAdd 事件處理常式附加至 ThisDocument 類別的 ContentControlAfterAdd 事件。

Private Sub ThisDocument_BuildingBlockContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlBuildingBlockGallery Then
        Me.Controls.AddBuildingBlockGalleryContentControl(NewContentControl, _
            "BuildingBlockControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_BuildingBlockContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
    {
        this.Controls.AddBuildingBlockGalleryContentControl(NewContentControl,
            "BuildingBlockControl" + NewContentControl.ID);
    }
}

以 .NET Framework 4 或 .NET Framework 4.5的這個版本是應用程式層級增益集。若要使用這段程式碼,請將它貼到專案的 ThisAddIn 類別中。此外,您還必須將 ActiveDocument_BuildingBlockContentControlAfterAdd 事件處理常式附加至現用文件的 ContentControlAfterAdd 事件。

Private Sub ActiveDocument_BuildingBlockContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlBuildingBlockGallery Then
        vstoDoc.Controls.AddBuildingBlockGalleryContentControl(NewContentControl, _
            "BuildingBlockControl" + NewContentControl.ID)
    End If
End Sub
void ActiveDocument_BuildingBlockContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
    {
        vstoDoc.Controls.AddBuildingBlockGalleryContentControl(NewContentControl,
            "BuildingBlockControl" + NewContentControl.ID);
    }
}

.NET Framework 安全性

請參閱

參考

ControlCollection 介面

AddBuildingBlockGalleryContentControl 多載

Microsoft.Office.Tools.Word 命名空間

其他資源

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

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