ControlCollection.AddGroupContentControl 方法

定义

重载

AddGroupContentControl(String)

在文档的当前选定内容中添加一个新的 GroupContentControl

AddGroupContentControl(ContentControl, String)

添加一个新的 GroupContentControl,它基于文档中的本机内容控件。

AddGroupContentControl(Range, String)

在文档的指定范围中,添加一个新的 GroupContentControl

AddGroupContentControl(String)

在文档的当前选定内容中添加一个新的 GroupContentControl

public:
 Microsoft::Office::Tools::Word::GroupContentControl ^ AddGroupContentControl(System::String ^ name);
public Microsoft.Office.Tools.Word.GroupContentControl AddGroupContentControl (string name);
abstract member AddGroupContentControl : string -> Microsoft.Office.Tools.Word.GroupContentControl
Public Function AddGroupContentControl (name As String) As GroupContentControl

参数

name
String

新控件的名称。

返回

GroupContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例将一个新段落添加到文档的开头,并创建一个包含此段落的新 GroupContentControl 段落。 阻止 GroupContentControl 用户编辑段落中的文本。 有关使用 GroupContentControl 来保护文档的一部分的详细信息,请参阅 内容控件

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddGroupControlAtSelection 方法。

private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;

private void AddGroupControlAtSelection()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    Word.Range range1 = this.Paragraphs[1].Range;
    range1.Text = "You cannot edit or change the formatting of text " +
        "in this paragraph, because this paragraph is in a GroupContentControl.";
    range1.Select();

    groupControl1 = this.Controls.AddGroupContentControl("groupControl1");
}
Dim groupControl1 As Microsoft.Office.Tools.Word.GroupContentControl

Private Sub AddGroupControlAtSelection()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Me.Paragraphs(1).Range.Text = "You cannot edit or change the formatting of text " & _
            "in this paragraph, because this paragraph is in a GroupContentControl."
    Me.Paragraphs(1).Range.Select()
    groupControl1 = Me.Controls.AddGroupContentControl("groupControl1")
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddGroupControlAtSelection 方法。

private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    Word.Range range1 = vstoDoc.Paragraphs[1].Range;
    range1.Text = "You cannot edit or change the formatting of text " +
        "in this paragraph, because this paragraph is in a GroupContentControl.";
    range1.Select();

    groupControl1 = vstoDoc.Controls.AddGroupContentControl("groupControl1");
}
Dim groupControl1 As Microsoft.Office.Tools.Word.GroupContentControl

Private Sub AddGroupControlAtSelection()
    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()
    vstoDoc.Paragraphs(1).Range.Text = "You cannot edit or change the formatting of text " & _
            "in this paragraph, because this paragraph is in a GroupContentControl."
    vstoDoc.Paragraphs(1).Range.Select()
    groupControl1 = vstoDoc.Controls.AddGroupContentControl("groupControl1")
End Sub

注解

使用此方法在运行时在文档中的当前选定内容中添加一个新 GroupContentControl 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddGroupContentControl(ContentControl, String)

添加一个新的 GroupContentControl,它基于文档中的本机内容控件。

public:
 Microsoft::Office::Tools::Word::GroupContentControl ^ AddGroupContentControl(Microsoft::Office::Interop::Word::ContentControl ^ contentControl, System::String ^ name);
public Microsoft.Office.Tools.Word.GroupContentControl AddGroupContentControl (Microsoft.Office.Interop.Word.ContentControl contentControl, string name);
abstract member AddGroupContentControl : Microsoft.Office.Interop.Word.ContentControl * string -> Microsoft.Office.Tools.Word.GroupContentControl
Public Function AddGroupContentControl (contentControl As ContentControl, name As String) As GroupContentControl

参数

contentControl
ContentControl

一个 ContentControl,它是新控件的基础。

name
String

新控件的名称。

返回

GroupContentControl,已添加到该文档。

例外

contentControlnull.-或 - name is null 或 具有零长度。

ControlCollection 中已存在具有相同名称的控件。

contentControl不是构建基块库 (也就是说, TypecontentControl 属性没有值 Microsoft.Office.Interop.Word。WdContentControlType.wdContentControlGroup) 。

示例

下面的代码示例为文档中的每个本机组控件创建一个新的 GroupContentControl

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 CreateGroupControlsFromNativeControls 方法。

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.GroupContentControl> groupControls;

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

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

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlGroup)
        {
            count++;
            Microsoft.Office.Tools.Word.GroupContentControl tempControl =
                this.Controls.AddGroupContentControl(nativeControl,
                "VSTOGroupControl" + count.ToString());
            groupControls.Add(tempControl);
        }
    }
}
Private groupControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.GroupContentControl)

Private Sub CreateGroupControlsFromNativeControls()
    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.wdContentControlGroup Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.GroupContentControl = _
                Me.Controls.AddGroupContentControl(nativeControl, _
                "VSTOGroupContentControl" + count.ToString())
            groupControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 CreateGroupControlsFromNativeControls 方法。

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.GroupContentControl> groupControls;

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

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

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

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlGroup)
        {
            count++;
            Microsoft.Office.Tools.Word.GroupContentControl tempControl =
                vstoDoc.Controls.AddGroupContentControl(nativeControl,
                "VSTOGroupControl" + count.ToString());
            groupControls.Add(tempControl);
        }
    }
}
Private groupControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.GroupContentControl)

Private Sub CreateGroupControlsFromNativeControls()
    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.wdContentControlGroup Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.GroupContentControl = _
                vstoDoc.Controls.AddGroupContentControl(nativeControl, _
                "VSTOGroupContentControl" + count.ToString())
            groupControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

下面的代码示例为用户添加到文档的每个本机组控件创建一个新的 GroupContentControl

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到 ThisDocument 项目中的 类中。 对于 C#,还必须将 ThisDocument_GroupContentControlAfterAdd 事件处理程序附加到 ContentControlAfterAdd 类的 ThisDocument 事件。

void ThisDocument_GroupContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlGroup)
    {
        this.Controls.AddGroupContentControl(NewContentControl,
            "GroupControl" + NewContentControl.ID);
    }
}
Private Sub ThisDocument_GroupContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlGroup Then
        Me.Controls.AddGroupContentControl(NewContentControl, _
            "GroupControl" + NewContentControl.ID)
    End If
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到 ThisAddIn 项目中的 类中。 此外,必须将事件处理程序附加到ActiveDocument_GroupContentControlAfterAddContentControlAfterAdd活动文档的 事件。

void ActiveDocument_GroupContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlGroup)
    {
        vstoDoc.Controls.AddGroupContentControl(NewContentControl,
            "GroupControl" + NewContentControl.ID);
    }
}
Private Sub ActiveDocument_GroupContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlGroup Then
        vstoDoc.Controls.AddGroupContentControl(NewContentControl, _
            "GroupControl" + NewContentControl.ID)
    End If
End Sub

注解

使用此方法在运行时添加基于文档中本机内容控件的新 GroupContentControl 。 当你在运行时创建 , GroupContentControl 并且你希望在下次打开文档时重新创建同一个控件时,这非常有用。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddGroupContentControl(Range, String)

在文档的指定范围中,添加一个新的 GroupContentControl

public:
 Microsoft::Office::Tools::Word::GroupContentControl ^ AddGroupContentControl(Microsoft::Office::Interop::Word::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Word.GroupContentControl AddGroupContentControl (Microsoft.Office.Interop.Word.Range range, string name);
abstract member AddGroupContentControl : Microsoft.Office.Interop.Word.Range * string -> Microsoft.Office.Tools.Word.GroupContentControl
Public Function AddGroupContentControl (range As Range, name As String) As GroupContentControl

参数

range
Range

Range,可为新控件提供边界。

name
String

新控件的名称。

返回

GroupContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例将一个新段落添加到文档的开头,并创建一个 GroupContentControl 包含此段落的 。 阻止 GroupContentControl 用户编辑段落中的文本。 有关使用 GroupContentControl 来保护文档的一部分的详细信息,请参阅 内容控件

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddGroupControlAtRange 方法。

private Microsoft.Office.Tools.Word.GroupContentControl groupControl2;

private void AddGroupControlAtRange()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    Word.Range range1 = this.Paragraphs[1].Range;
    range1.Text = "You cannot edit or change the formatting of text " +
        "in this paragraph, because this paragraph is in a GroupContentControl.";
    range1.Select();

    groupControl2 = this.Controls.AddGroupContentControl(range1, "groupControl2");
}
Dim groupControl2 As Microsoft.Office.Tools.Word.GroupContentControl

Private Sub AddGroupControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Dim range1 As Word.Range = Me.Paragraphs(1).Range
    range1.Text = "You cannot edit or change the formatting of text " & _
            "in this paragraph, because this paragraph is in a GroupContentControl."
    range1.Select()
    groupControl2 = Me.Controls.AddGroupContentControl(range1, "groupControl2")
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddGroupControlAtRange 方法。

private Microsoft.Office.Tools.Word.GroupContentControl groupControl2;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    Word.Range range1 = vstoDoc.Paragraphs[1].Range;
    range1.Text = "You cannot edit or change the formatting of text " +
        "in this paragraph, because this paragraph is in a GroupContentControl.";
    range1.Select();

    groupControl2 = vstoDoc.Controls.AddGroupContentControl(range1, "groupControl2");
}
Dim groupControl2 As Microsoft.Office.Tools.Word.GroupContentControl

Private Sub AddGroupControlAtRange()
    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()
    Dim range1 As Word.Range = vstoDoc.Paragraphs(1).Range
    range1.Text = "You cannot edit or change the formatting of text " & _
            "in this paragraph, because this paragraph is in a GroupContentControl."
    range1.Select()
    groupControl2 = vstoDoc.Controls.AddGroupContentControl(range1, "groupControl2")
End Sub

注解

使用此方法在运行时在文档中的指定范围内添加新 GroupContentControl 的 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于