Share via


ControlCollection.AddBuildingBlockGalleryContentControl (Método) (ContentControl, String)

Agrega un nuevo objeto BuildingBlockGalleryContentControl a la colección. El nuevo control se basa en un control de contenido nativo que ya está en el documento.

Espacio de nombres:  Microsoft.Office.Tools.Word
Ensamblado:  Microsoft.Office.Tools.Word (en Microsoft.Office.Tools.Word.dll)

Sintaxis

'Declaración
Function AddBuildingBlockGalleryContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As BuildingBlockGalleryContentControl
BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl(
    ContentControl contentControl,
    string name
)

Parámetros

Valor devuelto

Tipo: Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
El control BuildingBlockGalleryContentControl que se agregó al documento.

Excepciones

Excepción Condición
ArgumentNullException

El valor de contentControl es nullreferencia null (Nothing en Visual Basic).

O bien

name es nullreferencia null (Nothing en Visual Basic) o tiene longitud cero.

ControlNameAlreadyExistsException

Ya existe un control con el mismo nombre en ControlCollection.

ArgumentException

contentControl no es una galería de bloques de creación (es decir, la propiedad Type de contentControl no tiene el valor Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlBuildingBlockGallery).

Comentarios

Utilice este método para agregar un nuevo control BuildingBlockGalleryContentControl basado en un control de contenido nativo del documento. Esto resulta útil si crea un control BuildingBlockGalleryContentControl en tiempo de ejecución y desea volver a crear el mismo control la próxima vez que se abra el documento. Para obtener más información, vea Agregar controles a documentos de Office en tiempo de ejecución.

Ejemplos

En el ejemplo de código siguiente se crea un nuevo BuildingBlockGalleryContentControl para cada galería de bloques de creación nativos que ya está en el documento.

Se trata de una versión para una personalización en el nivel del documento. Para usar este código, péguelo en la clase ThisDocument del proyecto y llame al método CreateBuildingBlockControlsFromNativeControls desde el método ThisDocument_Startup.

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 versión es para un complemento de nivel de aplicación dirigido a .NET Framework 4. Para usar este código, péguelo en la clase ThisAddIn del proyecto de complemento y llame al método CreateBuildingBlockControlsFromNativeControls desde el método ThisAddIn_Startup.

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

En el ejemplo de código siguiente se crea un nuevo BuildingBlockGalleryContentControl para cada galería de bloques de creación nativos que el usuario agrega al documento.

Se trata de una versión para una personalización en el nivel del documento. Para usar este código, péguelo en la clase ThisDocument del proyecto. En C#, además debe asociar el controlador de eventos ThisDocument_BuildingBlockContentControlAfterAdd al evento ContentControlAfterAdd de la clase ThisDocument.

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 versión es para un complemento de nivel de aplicación dirigido a .NET Framework 4. Para usar este código, péguelo en la clase ThisAddIn del proyecto. Además, debe asociar el controlador de eventos ActiveDocument_BuildingBlockContentControlAfterAdd al evento ContentControlAfterAdd del documento activo.

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

Seguridad de .NET Framework

Vea también

Referencia

ControlCollection Interfaz

AddBuildingBlockGalleryContentControl (Sobrecarga)

Microsoft.Office.Tools.Word (Espacio de nombres)

Otros recursos

Agregar controles a documentos de Office en tiempo de ejecución

Métodos auxiliares para controles host

Cómo: Agregar controles de contenido a documentos de Word