Freigeben über


DocumentBase.SelectContentControlsByTag(String) Method

Definition

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

public:
 Microsoft::Office::Interop::Word::ContentControls ^ SelectContentControlsByTag(System::String ^ tag);
public Microsoft.Office.Interop.Word.ContentControls SelectContentControlsByTag (string tag);
member this.SelectContentControlsByTag : string -> Microsoft.Office.Interop.Word.ContentControls
Public Function SelectContentControlsByTag (tag As String) As ContentControls

Parameters

tag
String

The tag of the content controls to return.

Returns

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 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);
    }
}
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

Applies to