ControlCollection.AddBuildingBlockGalleryContentControl 方法

定义

重载

AddBuildingBlockGalleryContentControl(String)

在文档的当前选定内容中添加一个新的 BuildingBlockGalleryContentControl

AddBuildingBlockGalleryContentControl(ContentControl, String)

将新 BuildingBlockGalleryContentControl 添加到集合中。 新控件基于文档中已存在的本机内容控件。

AddBuildingBlockGalleryContentControl(Range, String)

在文档的指定范围中,添加一个新的 BuildingBlockGalleryContentControl

AddBuildingBlockGalleryContentControl(String)

在文档的当前选定内容中添加一个新的 BuildingBlockGalleryContentControl

public:
 Microsoft::Office::Tools::Word::BuildingBlockGalleryContentControl ^ AddBuildingBlockGalleryContentControl(System::String ^ name);
public Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl (string name);
abstract member AddBuildingBlockGalleryContentControl : string -> Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
Public Function AddBuildingBlockGalleryContentControl (name As String) As BuildingBlockGalleryContentControl

参数

name
String

新控件的名称。

返回

BuildingBlockGalleryContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例将新的 BuildingBlockGalleryContentControl 添加到文档的开头。 显示 BuildingBlockGalleryContentControl Microsoft Office Word提供的公式构建基块。

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddBuildingBlockControlAtSelection 方法。

private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl1;

private void AddBuildingBlockControlAtSelection()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    this.Paragraphs[1].Range.Select();

    buildingBlockControl1 = this.Controls.AddBuildingBlockGalleryContentControl(
        "buildingBlockControl1");
    buildingBlockControl1.PlaceholderText = "Choose an equation";
    buildingBlockControl1.BuildingBlockCategory = "Built-In";
    buildingBlockControl1.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations;
}
Dim buildingBlockGalleryControl1 As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl

Private Sub AddBuildingBlockGalleryControlAtSelection()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Me.Paragraphs(1).Range.Select()
    buildingBlockGalleryControl1 = Me.Controls.AddBuildingBlockGalleryContentControl( _
        "buildingBlockGalleryControl1")
    With buildingBlockGalleryControl1
        .PlaceholderText = "Choose an equation"
        .BuildingBlockCategory = "Built-In"
        .BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations
    End With
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddBuildingBlockControlAtSelection 方法。

private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl1;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    vstoDoc.Paragraphs[1].Range.Select();

    buildingBlockControl1 = vstoDoc.Controls.AddBuildingBlockGalleryContentControl(
        "buildingBlockControl1");
    buildingBlockControl1.PlaceholderText = "Choose an equation";
    buildingBlockControl1.BuildingBlockCategory = "Built-In";
    buildingBlockControl1.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations;
}
Dim buildingBlockGalleryControl1 As BuildingBlockGalleryContentControl

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    vstoDoc.Paragraphs(1).Range.Select()
    buildingBlockGalleryControl1 = vstoDoc.Controls.AddBuildingBlockGalleryContentControl( _
        "buildingBlockGalleryControl1")
    With buildingBlockGalleryControl1
        .PlaceholderText = "Choose an equation"
        .BuildingBlockCategory = "Built-In"
        .BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations
    End With
End Sub

注解

使用此方法在运行时在文档中的当前选定内容处添加新 BuildingBlockGalleryContentControl 的 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddBuildingBlockGalleryContentControl(ContentControl, String)

将新 BuildingBlockGalleryContentControl 添加到集合中。 新控件基于文档中已存在的本机内容控件。

public:
 Microsoft::Office::Tools::Word::BuildingBlockGalleryContentControl ^ AddBuildingBlockGalleryContentControl(Microsoft::Office::Interop::Word::ContentControl ^ contentControl, System::String ^ name);
public Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl (Microsoft.Office.Interop.Word.ContentControl contentControl, string name);
abstract member AddBuildingBlockGalleryContentControl : Microsoft.Office.Interop.Word.ContentControl * string -> Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
Public Function AddBuildingBlockGalleryContentControl (contentControl As ContentControl, name As String) As BuildingBlockGalleryContentControl

参数

contentControl
ContentControl

一个 ContentControl,它是新控件的基础。

name
String

新控件的名称。

返回

BuildingBlockGalleryContentControl,已添加到该文档。

例外

contentControlnull.-或- name is null 或 具有零长度。

ControlCollection 中已存在具有相同名称的控件。

contentControl不是构建基块库 (也就是说, TypecontentControl 属性没有值 Microsoft.Office.Interop.Word。WdContentControlType.wdContentControlBuildingBlockGallery) 。

示例

下面的代码示例为文档中已有的每个本机构建基块库创建一个新的 BuildingBlockGalleryContentControl

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 CreateBuildingBlockControlsFromNativeControls 方法。

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

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn外接程序项目中的 类中,然后从 ThisAddIn_Startup 方法调用 CreateBuildingBlockControlsFromNativeControls 方法。

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

下面的代码示例为用户添加到文档的每个本机构建基块库创建一个新的 BuildingBlockGalleryContentControl

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到 ThisDocument 项目中的 类中。 对于 C#,还必须将 ThisDocument_BuildingBlockContentControlAfterAdd 事件处理程序附加到 ContentControlAfterAdd 类的 ThisDocument 事件。

void ThisDocument_BuildingBlockContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
    {
        this.Controls.AddBuildingBlockGalleryContentControl(NewContentControl,
            "BuildingBlockControl" + NewContentControl.ID);
    }
}
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

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到 ThisAddIn 项目中的 类中。 此外,必须将事件处理程序附加到ActiveDocument_BuildingBlockContentControlAfterAddContentControlAfterAdd活动文档的 事件。

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

注解

使用此方法可添加基于文档中本机内容控件的新 BuildingBlockGalleryContentControl 。 如果在运行时创建 , BuildingBlockGalleryContentControl 并且希望在下次打开文档时重新创建同一控件,这非常有用。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddBuildingBlockGalleryContentControl(Range, String)

在文档的指定范围中,添加一个新的 BuildingBlockGalleryContentControl

public:
 Microsoft::Office::Tools::Word::BuildingBlockGalleryContentControl ^ AddBuildingBlockGalleryContentControl(Microsoft::Office::Interop::Word::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl (Microsoft.Office.Interop.Word.Range range, string name);
abstract member AddBuildingBlockGalleryContentControl : Microsoft.Office.Interop.Word.Range * string -> Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
Public Function AddBuildingBlockGalleryContentControl (range As Range, name As String) As BuildingBlockGalleryContentControl

参数

range
Range

Range,可为新控件提供边界。

name
String

新控件的名称。

返回

BuildingBlockGalleryContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例将新的 BuildingBlockGalleryContentControl 添加到文档的开头。 显示 BuildingBlockGalleryContentControl Microsoft Office Word提供的公式构建基块。

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddBuildingBlockControlAtRange 方法。

private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl2;

private void AddBuildingBlockControlAtRange()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();

    buildingBlockControl2 = this.Controls.AddBuildingBlockGalleryContentControl(
       this.Paragraphs[1].Range, "buildingBlockControl2");
    buildingBlockControl2.PlaceholderText = "Choose an equation";
    buildingBlockControl2.BuildingBlockCategory = "Built-In";
    buildingBlockControl2.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations;
}
Dim buildingBlockGalleryControl2 As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl

Private Sub AddBuildingBlockGalleryControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    buildingBlockGalleryControl2 = Me.Controls.AddBuildingBlockGalleryContentControl( _
        Me.Paragraphs(1).Range, "buildingBlockGalleryControl2")
    With buildingBlockGalleryControl2
        .PlaceholderText = "Choose an equation"
        .BuildingBlockCategory = "Built-In"
        .BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations
    End With
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddBuildingBlockControlAtRange 方法。

private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl2;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();

    buildingBlockControl2 = vstoDoc.Controls.AddBuildingBlockGalleryContentControl(
       vstoDoc.Paragraphs[1].Range, "buildingBlockControl2");
    buildingBlockControl2.PlaceholderText = "Choose an equation";
    buildingBlockControl2.BuildingBlockCategory = "Built-In";
    buildingBlockControl2.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations;
}
Dim buildingBlockGalleryControl2 As BuildingBlockGalleryContentControl

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    buildingBlockGalleryControl2 = vstoDoc.Controls.AddBuildingBlockGalleryContentControl( _
        vstoDoc.Paragraphs(1).Range, "buildingBlockGalleryControl2")
    With buildingBlockGalleryControl2
        .PlaceholderText = "Choose an equation"
        .BuildingBlockCategory = "Built-In"
        .BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeEquations
    End With
End Sub

注解

使用此方法在运行时在文档中的指定区域添加新 BuildingBlockGalleryContentControl 的 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于