共用方式為


ControlCollection.AddComboBoxContentControl 方法 (Range, String)

將新的 ComboBoxContentControl 加入至文件中指定的範圍內。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)

語法

'宣告
Function AddComboBoxContentControl ( _
    range As Range, _
    name As String _
) As ComboBoxContentControl
ComboBoxContentControl AddComboBoxContentControl(
    Range range,
    string name
)

參數

傳回值

型別:Microsoft.Office.Tools.Word.ComboBoxContentControl
加入至文件中的 ComboBoxContentControl

例外狀況

例外狀況 條件
ArgumentNullException

name 是 nullnull 參考 (即 Visual Basic 中的 Nothing),或長度為 0。

ControlNameAlreadyExistsException

ControlCollection 中已有相同名稱的控制項。

備註

在執行階段使用這個方法,將新的 ComboBoxContentControl 加入至文件中指定的範圍。 如需詳細資訊,請參閱在執行階段將控制項加入至 Office 文件

範例

下列程式碼範例會將新的 ComboBoxContentControl 加入至文件的開頭。 此範例也將幾個色彩名稱加入至使用者可在控制項中選取的項目清單中。

這是示範文件層級自訂的版本。 若要使用這段程式碼,請將它貼到專案的 ThisDocument 類別中,並從 ThisDocument_Startup 方法呼叫 AddComboBoxControlAtRange 方法。

Dim comboBoxControl2 As Microsoft.Office.Tools.Word.ComboBoxContentControl

Private Sub AddComboBoxControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    comboBoxControl2 = Me.Controls.AddComboBoxContentControl(Me.Paragraphs(1).Range, "comboBoxControl2")
    With comboBoxControl2
        .DropDownListEntries.Add("Red", "Red", 0)
        .DropDownListEntries.Add("Green", "Green", 1)
        .DropDownListEntries.Add("Blue", "Blue", 2)
        .PlaceholderText = "Choose a color, or enter your own"
    End With
End Sub
private Microsoft.Office.Tools.Word.ComboBoxContentControl comboBoxControl2;

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

    comboBoxControl2 = this.Controls.AddComboBoxContentControl(this.Paragraphs[1].Range,
        "comboBoxControl2");
    comboBoxControl2.DropDownListEntries.Add("Red", "Red", 0);
    comboBoxControl2.DropDownListEntries.Add("Green", "Green", 1);
    comboBoxControl2.DropDownListEntries.Add("Blue", "Blue", 2);
    comboBoxControl2.PlaceholderText = "Choose a color, or enter your own";
}

這個版本適用於以 .NET Framework 4 或 .NET Framework 4.5 為目標的應用程式層級增益集。 若要使用這段程式碼,請將它貼到專案的 ThisAddIn 類別中,並從 ThisAddIn_Startup 方法呼叫 AddComboBoxControlAtRange 方法。

Dim comboBoxControl2 As Microsoft.Office.Tools.Word.ComboBoxContentControl

Private Sub AddComboBoxControlAtRange()
    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()
    comboBoxControl2 = vstoDoc.Controls.AddComboBoxContentControl( _
        vstoDoc.Paragraphs(1).Range, "comboBoxControl2")
    With comboBoxControl2
        .DropDownListEntries.Add("Red", "Red", 0)
        .DropDownListEntries.Add("Green", "Green", 1)
        .DropDownListEntries.Add("Blue", "Blue", 2)
        .PlaceholderText = "Choose a color, or enter your own"
    End With
End Sub
private Microsoft.Office.Tools.Word.ComboBoxContentControl comboBoxControl2;

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

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

    comboBoxControl2 = vstoDoc.Controls.AddComboBoxContentControl(
        vstoDoc.Paragraphs[1].Range,
        "comboBoxControl2");
    comboBoxControl2.DropDownListEntries.Add("Red", "Red", 0);
    comboBoxControl2.DropDownListEntries.Add("Green", "Green", 1);
    comboBoxControl2.DropDownListEntries.Add("Blue", "Blue", 2);
    comboBoxControl2.PlaceholderText = "Choose a color, or enter your own";            
}

.NET Framework 安全性

請參閱

參考

ControlCollection 介面

AddComboBoxContentControl 多載

Microsoft.Office.Tools.Word 命名空間

其他資源

在執行階段將控制項加入至 Office 文件

如何:將內容控制項加入至 Word 文件