Método ControlCollection.AddBuildingBlockGalleryContentControl (ContentControl, String)

Adiciona um novo BuildingBlockGalleryContentControl à coleção. O novo controle baseia-se em um controle de conteúdo nativo que já está no documento.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word (em Microsoft.Office.Tools.Word.dll)

Sintaxe

'Declaração
Function AddBuildingBlockGalleryContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As BuildingBlockGalleryContentControl
BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl(
    ContentControl contentControl,
    string name
)

Parâmetros

Valor de retorno

Tipo: Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
O BuildingBlockGalleryContentControl que foi adicionado ao documento.

Exceções

Exceção Condição
ArgumentNullException

contentControl is nulluma referência nula (Nothing no Visual Basic).

- ou -

nameé nulluma referência nula (Nothing no Visual Basic) ou tem comprimento zero.

ControlNameAlreadyExistsException

Um controle com o mesmo nome já está na ControlCollection.

ArgumentException

contentControlnão é uma Galeria de bloco de construção (isto é, o Type propriedade de contentControl não tem o valor Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlBuildingBlockGallery).

Comentários

Use esse método para adicionar um novo BuildingBlockGalleryContentControl que se baseia em um controle de conteúdo nativo no documento. Isso é útil quando você cria um BuildingBlockGalleryContentControl em tempo de execução, e você quiser recriar o mesmo controle na próxima vez em que o documento for aberto. For more information, see Adicionar controles a documentos do Office em tempo de execução.

Exemplos

O exemplo de código a seguir cria um novo BuildingBlockGalleryContentControl para a Galeria de cada bloco de construção nativas que está já no documento.

Esta versão é para uma personalização em nível de documento. Para usar esse código, colá-lo na ThisDocument classe em seu projeto e a chamada a CreateBuildingBlockControlsFromNativeControls método a partir do ThisDocument_Startup método.

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

Esta versão é um suplemento de nível de aplicativo que se destina a .NET Framework 4. Para usar esse código, colá-lo na ThisAddIn classe em seu projeto e a chamada a CreateBuildingBlockControlsFromNativeControls método a partir do ThisAddIn_Startup método.

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

O exemplo de código a seguir cria um novo BuildingBlockGalleryContentControl para cada Galeria de bloco de construção nativas que o usuário adiciona ao documento.

Esta versão é para uma personalização em nível de documento. Para usar esse código, colá-lo para o ThisDocument classe no projeto. Para C#, você também deve anexar o ThisDocument_BuildingBlockContentControlAfterAdd o manipulador de eventos para o ContentControlAfterAdd o evento da ThisDocument classe.

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

Esta versão é um suplemento de nível de aplicativo que se destina a .NET Framework 4. Para usar esse código, colá-lo para o ThisAddIn classe no projeto. Além disso, você deve anexar o ActiveDocument_BuildingBlockContentControlAfterAdd o manipulador de eventos para o ContentControlAfterAdd eventos do documento ativo.

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

Segurança do .NET Framework

Consulte também

Referência

ControlCollection Interface

Sobrecargas AddBuildingBlockGalleryContentControl

Namespace Microsoft.Office.Tools.Word

Outros recursos

Adicionar controles a documentos do Office em tempo de execução

Métodos auxiliares para controles de Host

Como: Adicionar controles de conteúdo para documentos do Word