更新: 2008 年 7 月
向新的 ComboBoxContentControl 添加到集合中。新控件基于文件中已有的本机内容控件。
命名空间:  Microsoft.Office.Tools.Word
程序集:  Microsoft.Office.Tools.Word.v9.0(在 Microsoft.Office.Tools.Word.v9.0.dll 中)
语法
声明
Public Function AddComboBoxContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As ComboBoxContentControl
用法
Dim instance As ControlCollection
Dim contentControl As ContentControl
Dim name As String
Dim returnValue As ComboBoxContentControl
returnValue = instance.AddComboBoxContentControl(contentControl, _
    name)
public ComboBoxContentControl AddComboBoxContentControl(
    ContentControl contentControl,
    string name
)
参数
- contentControl 
 类型:Microsoft.Office.Interop.Word.ContentControl- 作为新控件的基础的 Microsoft.Office.Interop.Word.ContentControl。 
- name 
 类型:System.String- 新控件的名称。 
返回值
类型:Microsoft.Office.Tools.Word.ComboBoxContentControl
添加到文档的 ComboBoxContentControl。
异常
| 异常 | 条件 | 
|---|---|
| ArgumentNullException | contentControl 为 nullnull 引用(在 Visual Basic 中为 Nothing)。 - 或 - name 为 nullnull 引用(在 Visual Basic 中为 Nothing) 或长度为零。 | 
| ControlNameAlreadyExistsException | ControlCollection 中已存在一个同名控件。 | 
| ArgumentException | contentControl 不是生成块库(即 contentControl 的 Type 属性不具有 Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlComboBox 值)。 | 
备注
使用此方法,可以在运行时添加基于文档中的本机内容控件的新 ComboBoxContentControl。如果在运行时创建 ComboBoxContentControl,并且希望在下次打开文档时重新创建相同的控件,这非常有用。有关更多信息,请参见在运行时向 Office 文档添加控件。
示例
下面的代码示例会为文档中已有的每个本机组合框创建一个新的 ComboBoxContentControl。
此版本针对的是文档级自定义项。若要使用此代码,请将其粘贴到项目内的 ThisDocument 类中,然后从 ThisDocument_Startup 方法中调用 CreateComboBoxControlsFromNativeControls 方法。
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);
        }
    }
}
此版本针对的是应用程序级外接程序。若要使用此代码,请将其粘贴到项目内的 ThisAddIn 类中,然后从 ThisAddIn_Startup 方法中调用 CreateComboBoxControlsFromNativeControls 方法。
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 = Me.Application.ActiveDocument.GetVstoObject()
    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 = this.Application.ActiveDocument.GetVstoObject();
    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);
        }
    }
}
下面的代码示例会为用户向文档中添加的每个本机组合框创建一个新的 ComboBoxContentControl。
此版本针对的是文档级自定义项。若要使用此代码,请将其粘贴到项目内的 ThisDocument 类中。对于 C#,还必须将 ThisDocument_ComboBoxContentControlAfterAdd 事件处理程序附加到 ThisDocument 类的 ContentControlAfterAdd 事件。
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);
    }
}
此版本针对的是应用程序级外接程序。若要使用此代码,请将其粘贴到项目内的 ThisAddIn 类中。此外,还必须将 ActiveDocument_ComboBoxContentControlAfterAdd 事件处理程序附加到活动文档的 ContentControlAfterAdd 事件。
Private Sub ActiveDocument_ComboBoxContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)
    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    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 = this.Application.ActiveDocument.GetVstoObject();
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlComboBox)
    {
        vstoDoc.Controls.AddComboBoxContentControl(NewContentControl,
            "ComboBoxControl" + NewContentControl.ID);
    }
}
权限
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。
另请参见
参考
Microsoft.Office.Tools.Word 命名空间
其他资源
修订记录
| 日期 | 修订记录 | 原因 | 
|---|---|---|
| 2008 年 7 月 | 添加了多个针对应用程序级外接程序的代码示例版本。 | SP1 功能更改。 |