Compartilhar via


DocumentBase.SelectContentControlsByTag Method

Returns all the content controls in the document that have the specified tag.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word.v4.0.Utilities (in Microsoft.Office.Tools.Word.v4.0.Utilities.dll)

Syntax

'Declaration
Public Function SelectContentControlsByTag ( _
    tag As String _
) As ContentControls
public ContentControls SelectContentControlsByTag(
    string tag
)

Parameters

  • tag
    Type: System.String

    The tag of the content controls to return.

Return Value

Type: Microsoft.Office.Interop.Word.ContentControls
A ContentControls collection that contains the content controls that have the specified tag.

Examples

The following code example adds three paragraphs to the document and then adds one control to each new paragraph: a RichTextContentControl, a ComboBoxContentControl, and a DropDownListContentControl. The example sets the Tag and Title properties of each control. Next, the code calls the SelectContentControlsByTag method to get the collection of native content controls whose tag value equals Customer and then displays the title of each control from the returned collection in a message box. To use this example, run it from the ThisDocument class in a document-level project.

Private Sub ContentControlsTag()
    Dim par1 As Word.Paragraph = Me.Paragraphs.Add()
    Dim richTextControl As  _
        Microsoft.Office.Tools.Word.RichTextContentControl = _
        Me.Controls.AddRichTextContentControl(par1.Range, "richTextControl")
    richTextControl.Tag = "Customer"
    richTextControl.Title = "Customer Name" 

    Dim par2 As Word.Paragraph = Me.Paragraphs.Add()
    Dim comboBoxControl As  _
        Microsoft.Office.Tools.Word.ComboBoxContentControl = _
        Me.Controls.AddComboBoxContentControl(par2.Range, "comboBoxControl")
    comboBoxControl.Tag = "Customer"
    comboBoxControl.Title = "Customer Title" 

    Dim par3 As Word.Paragraph = Me.Paragraphs.Add()
    Dim dropDownListControl As  _
        Microsoft.Office.Tools.Word.DropDownListContentControl = _
        Me.Controls.AddDropDownListContentControl(par3.Range, _
            "dropDownListControl")
    dropDownListControl.Tag = "Products"
    dropDownListControl.Title = "List of Products" 

    Dim relatedControls As Word.ContentControls = _
        Me.SelectContentControlsByTag("Customer")
    MessageBox.Show("Displaying all controls with a Tag value of" + _
                    " 'Customer'. Click OK to continue.")
    For Each ctrl As Word.ContentControl In relatedControls
        MessageBox.Show("Control title: " + ctrl.Title)
    Next 
End Sub
private void ContentControlsTag()
{
    Word.Paragraph par1 = this.Paragraphs.Add(ref missing);
    Microsoft.Office.Tools.Word.RichTextContentControl richTextControl =
        this.Controls.AddRichTextContentControl(par1.Range, 
        "richTextControl");
    richTextControl.Tag = "Customer";
    richTextControl.Title = "Customer Name";

    Word.Paragraph par2 = this.Paragraphs.Add(ref missing);
    Microsoft.Office.Tools.Word.ComboBoxContentControl comboBoxControl =
        this.Controls.AddComboBoxContentControl(par2.Range, 
        "comboBoxControl");
    comboBoxControl.Tag = "Customer";
    comboBoxControl.Title = "Customer Title";

    Word.Paragraph par3 = this.Paragraphs.Add(ref missing);
    Microsoft.Office.Tools.Word.DropDownListContentControl 
        dropDownListControl = this.Controls.AddDropDownListContentControl(
        par3.Range, "dropDownListControl");
    dropDownListControl.Tag = "Products";
    dropDownListControl.Title = "List of Products";

    Word.ContentControls relatedControls = 
        this.SelectContentControlsByTag("Customer");
    MessageBox.Show("Displaying all controls with a Tag value of" + 
        " 'Customer'. Click OK to continue.");
    foreach (Word.ContentControl ctrl in relatedControls)
    {
        MessageBox.Show("Control title: " + ctrl.Title);
    }
}

.NET Framework Security

See Also

Reference

DocumentBase Class

Microsoft.Office.Tools.Word Namespace