Share via


ControlCollection.AddGroupContentControl Method

Definition

Overloads

AddGroupContentControl(String)

Adds a new GroupContentControl at the current selection in the document.

AddGroupContentControl(ContentControl, String)

Adds a new GroupContentControl that is based on a native content control in the document.

AddGroupContentControl(Range, String)

Adds a new GroupContentControl at the specified range in the document.

AddGroupContentControl(String)

Adds a new GroupContentControl at the current selection in the document.

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

Parameters

name
String

The name of the new control.

Returns

The GroupContentControl that was added to the document.

Exceptions

name is null or has zero length.

A control with the same name is already in the ControlCollection.

Examples

The following code example adds a new paragraph to the beginning of the document, and it creates a new GroupContentControl that contains this paragraph. The GroupContentControl prevents users from editing the text in the paragraph. For more information about using a GroupContentControl to protect a part of a document, see Content Controls.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the AddGroupControlAtSelection method from the ThisDocument_Startup method.

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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project, and call the AddGroupControlAtSelection method from the ThisAddIn_Startup method.

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

Remarks

Use this method to add a new GroupContentControl at the current selection in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.

Applies to

AddGroupContentControl(ContentControl, String)

Adds a new GroupContentControl that is based on a native content control in the document.

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

Parameters

contentControl
ContentControl

The ContentControl that is the basis for the new control.

name
String

The name of the new control.

Returns

The GroupContentControl that was added to the document.

Exceptions

contentControl is null.-or- name is null or has zero length.

A control with the same name is already in the ControlCollection.

contentControl is not a building block gallery (that is, the Type property of contentControl does not have the value Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlGroup).

Examples

The following code example creates a new GroupContentControl for every native group control that is in the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the CreateGroupControlsFromNativeControls method from the ThisDocument_Startup method.

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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project, and call the CreateGroupControlsFromNativeControls method from the ThisAddIn_Startup method.

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

The following code example creates a new GroupContentControl for every native group control that the user adds to the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project. For C#, you must also attach the ThisDocument_GroupContentControlAfterAdd event handler to the ContentControlAfterAdd event of the ThisDocument class.

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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project. Also, you must attach the ActiveDocument_GroupContentControlAfterAdd event handler to the ContentControlAfterAdd event of the active document.

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

Remarks

Use this method to add a new GroupContentControl that is based on a native content control in the document at run time. This is useful when you create a GroupContentControl at run time, and you want to recreate the same control the next time the document is opened. For more information, see Adding Controls to Office Documents at Run Time.

Applies to

AddGroupContentControl(Range, String)

Adds a new GroupContentControl at the specified range in the document.

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

Parameters

range
Range

A Range that provides the bounds for the new control.

name
String

The name of the new control.

Returns

The GroupContentControl that was added to the document.

Exceptions

name is null or has zero length.

A control with the same name is already in the ControlCollection.

Examples

The following code example adds a new paragraph to the beginning of the document, and it creates a GroupContentControl that contains this paragraph. The GroupContentControl prevents users from editing the text in the paragraph. For more information about using a GroupContentControl to protect a part of a document, see Content Controls.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the AddGroupControlAtRange method from the ThisDocument_Startup method.

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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project, and call the AddGroupControlAtRange method from the ThisAddIn_Startup method.

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

Remarks

Use this method to add a new GroupContentControl at a specified range in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.

Applies to