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) 或长度为零。

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 的应用程序级外接程序。 若要使用此代码,请将其粘贴到项目内的 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 文档添加内容控件