Share via


ControlCollection.AddBuildingBlockGalleryContentControl, méthode (ContentControl, String)

Ajoute un nouveau BuildingBlockGalleryContentControl à la collection. Le nouveau contrôle est basé sur un contrôle de contenu natif qui se trouve déjà dans le document.

Espace de noms :  Microsoft.Office.Tools.Word
Assembly :  Microsoft.Office.Tools.Word (dans Microsoft.Office.Tools.Word.dll)

Syntaxe

'Déclaration
Function AddBuildingBlockGalleryContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As BuildingBlockGalleryContentControl
BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl(
    ContentControl contentControl,
    string name
)

Paramètres

Valeur de retour

Type : Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl
BuildingBlockGalleryContentControl ajouté au document.

Exceptions

Exception Condition
ArgumentNullException

contentControl a la valeur nullune référence null (Nothing en Visual Basic).

ou

name est nullune référence null (Nothing en Visual Basic) ou une longueur nulle.

ControlNameAlreadyExistsException

Un contrôle du même nom figure déjà dans ControlCollection.

ArgumentException

contentControl n'est pas une galerie de blocs de construction (à savoir la propriété Type de contentControl n'a pas la valeur Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlBuildingBlockGallery).

Notes

Utilisez cette méthode pour ajouter un nouveau BuildingBlockGalleryContentControl basé sur un contrôle de contenu natif dans le document. Cela est utile lorsque vous créez un BuildingBlockGalleryContentControl au moment de l'exécution et que vous souhaitez recréer le même contrôle lors de la prochaine ouverture du document. Pour plus d'informations, consultez Ajout de contrôles à des documents Office au moment de l'exécution.

Exemples

L'exemple de code suivant crée un nouveau BuildingBlockGalleryContentControl pour chaque galerie de blocs de construction native figurant déjà dans le document.

Cette version est destinée à une personnalisation au niveau du document. Pour utiliser ce code, collez-le dans la classe ThisDocument de votre projet, puis appelez la méthode CreateBuildingBlockControlsFromNativeControls à partir de la méthode 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);
        }
    }
}

Cette version concerne un complément de niveau application qui cible le .NET Framework 4 ou le .NET Framework 4.5. Pour utiliser ce code, collez-le dans la classe ThisAddIn de votre projet de complément, puis appelez la méthode CreateBuildingBlockControlsFromNativeControls à partir de la méthode 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);
        }
    }
}

L'exemple de code suivant crée un nouveau BuildingBlockGalleryContentControl pour chaque galerie de blocs de construction native que l'utilisateur ajoute au document.

Cette version est destinée à une personnalisation au niveau du document. Pour utiliser ce code, collez-le dans la classe ThisDocument de votre projet. En C#, vous devez également attacher le gestionnaire d'événements ThisDocument_BuildingBlockContentControlAfterAdd à l'événement ContentControlAfterAdd de la classe 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);
    }
}

Cette version concerne un complément de niveau application qui cible le .NET Framework 4 ou le .NET Framework 4.5. Pour utiliser ce code, collez-le dans la classe ThisAddIn de votre projet. Vous devez également attacher le gestionnaire d'événements ActiveDocument_BuildingBlockContentControlAfterAdd à l'événement ContentControlAfterAdd du document actif.

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

Sécurité .NET Framework

Voir aussi

Référence

ControlCollection Interface

AddBuildingBlockGalleryContentControl, surcharge

Microsoft.Office.Tools.Word, espace de noms

Autres ressources

Ajout de contrôles à des documents Office au moment de l'exécution

Comment : ajouter des contrôles de contenu à des documents Word