ControlCollection.AddComboBoxContentControl-Methode (ContentControl, String)

Fügt der Auflistung ein neues ComboBoxContentControl hinzu. Das neue Steuerelement basiert auf einem systemeigenen Inhaltssteuerelement, das bereits im Dokument vorhanden ist.

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

Syntax

'Declaration
Function AddComboBoxContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As ComboBoxContentControl
ComboBoxContentControl AddComboBoxContentControl(
    ContentControl contentControl,
    string name
)

Parameter

Rückgabewert

Typ: Microsoft.Office.Tools.Word.ComboBoxContentControl
Das ComboBoxContentControl, das dem Dokument hinzugefügt wurde.

Ausnahmen

Ausnahme Bedingung
ArgumentNullException

contentControl hat den Wert nullNULL-Verweis (Nothing in Visual Basic).

– oder –

name ist nullNULL-Verweis (Nothing in Visual Basic) oder hat die Länge 0 (null).

ControlNameAlreadyExistsException

Ein Steuerelement mit dem gleichen Namen ist bereits in der ControlCollection vorhanden.

ArgumentException

contentControl ist kein Bausteinkatalog (d. h., die Type-Eigenschaft von contentControl hat nicht den Wert Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlComboBox).

Hinweise

Verwenden Sie diese Methode, um zur Laufzeit ein neues ComboBoxContentControl hinzuzufügen, das auf einem systemeigenen Inhaltssteuerelement im Dokument basiert. Diese Vorgehensweise ist hilfreich, wenn Sie ein ComboBoxContentControl zur Laufzeit erstellen und das gleiche Steuerelement beim nächsten Öffnen des Dokuments neu erstellen möchten. Weitere Informationen finden Sie unter Hinzufügen von Steuerelementen zu Office-Dokumenten zur Laufzeit.

Beispiele

Im folgenden Codebeispiel wird für jedes systemeigene Kombinationsfeld, das bereits im Dokument enthalten ist, ein neues ComboBoxContentControl erstellt.

Diese Version bezieht sich auf eine Anpassung auf Dokumentebene. Zum Verwenden dieses Codes fügen Sie ihn in der ThisDocument-Klasse in das Projekt ein und rufen in der ThisDocument_Startup-Methode die CreateComboBoxControlsFromNativeControls-Methode auf.

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

Private Sub CreateComboBoxControlsFromNativeControls()
    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.wdContentControlComboBox Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.ComboBoxContentControl = _
                Me.Controls.AddComboBoxContentControl(nativeControl, _
                "VSTOComboBoxContentControl" + count.ToString())
            comboBoxControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.ComboBoxContentControl> comboBoxControls;

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

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

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlComboBox)
        {
            count++;
            Microsoft.Office.Tools.Word.ComboBoxContentControl tempControl =
                this.Controls.AddComboBoxContentControl(nativeControl,
                "VSTOComboBoxContentControl" + count.ToString());
            comboBoxControls.Add(tempControl);
        }
    }
}

Diese Version bezieht sich auf ein Add-In auf Anwendungsebene, das auf .NET Framework 4 abzielt. Zum Verwenden dieses Codes fügen Sie ihn in der ThisAddIn-Klasse in das Projekt ein und rufen in der ThisAddIn_Startup-Methode die CreateComboBoxControlsFromNativeControls-Methode auf.

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

Private Sub CreateComboBoxControlsFromNativeControls()
    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.wdContentControlComboBox Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.ComboBoxContentControl = _
                vstoDoc.Controls.AddComboBoxContentControl(nativeControl, _
                "VSTOComboBoxContentControl" + count.ToString())
            comboBoxControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.ComboBoxContentControl> comboBoxControls;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (vstoDoc.ContentControls.Count <= 0)
        return;

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

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlComboBox)
        {
            count++;
            Microsoft.Office.Tools.Word.ComboBoxContentControl tempControl =
                vstoDoc.Controls.AddComboBoxContentControl(nativeControl,
                "VSTOComboBoxContentControl" + count.ToString());
            comboBoxControls.Add(tempControl);
        }
    }
}

Im folgenden Codebeispiel wird für jedes systemeigene Kombinationsfeld, das der Benutzer dem Dokument hinzufügt, ein neues ComboBoxContentControl erstellt.

Diese Version bezieht sich auf eine Anpassung auf Dokumentebene. Wenn Sie diesen Code verwenden möchten, fügen Sie ihn in der ThisDocument-Klasse im Projekt ein. In C# müssen Sie außerdem den ThisDocument_ComboBoxContentControlAfterAdd-Ereignishandler an das ContentControlAfterAdd-Ereignis der ThisDocument-Klasse anfügen.

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

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlComboBox Then
        Me.Controls.AddComboBoxContentControl(NewContentControl, _
            "ComboBoxControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_ComboBoxContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlComboBox)
    {
        this.Controls.AddComboBoxContentControl(NewContentControl,
            "ComboBoxControl" + NewContentControl.ID);
    }
}

Diese Version bezieht sich auf ein Add-In auf Anwendungsebene, das auf .NET Framework 4 abzielt. Wenn Sie diesen Code verwenden möchten, fügen Sie ihn in der ThisAddIn-Klasse im Projekt ein. Außerdem müssen Sie den ActiveDocument_ComboBoxContentControlAfterAdd-Ereignishandler an das ContentControlAfterAdd-Ereignis des aktiven Dokuments anfügen.

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

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

.NET Framework-Sicherheit

Siehe auch

Referenz

ControlCollection Schnittstelle

AddComboBoxContentControl-Überladung

Microsoft.Office.Tools.Word-Namespace

Weitere Ressourcen

Hinzufügen von Steuerelementen zu Office-Dokumenten zur Laufzeit

Hilfsmethoden für Hoststeuerelemente

Gewusst wie: Hinzufügen von Inhaltssteuerelementen zu Word-Dokumenten