共用方式為


DocumentBase.SelectContentControlsByTag 方法

傳回文件中具有特定標記 (Tag) 的所有內容控制項。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word.v4.0.Utilities (在 Microsoft.Office.Tools.Word.v4.0.Utilities.dll 中)

語法

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

參數

  • tag
    型別:System.String
    要傳回之內容控制項的標記。

傳回值

型別:Microsoft.Office.Interop.Word.ContentControls
ContentControls 集合,包含具有指定之標記的內容控制項。

範例

下列程式碼範例會在此文件中加入三個段落,然後在每個新段落中各加入一個控制項:RichTextContentControlComboBoxContentControlDropDownListContentControl。 這個範例會設定每個控制項的 Tag 和 Title 屬性。 接下來,程式碼會呼叫 SelectContentControlsByTag 方法,取得標籤值等於 Customer 之原生內容控制項的集合,然後在訊息方塊中顯示所傳回集合之各個控制項的標題。 若要使用這個範例,請在文件層級專案中的 ThisDocument 類別執行。

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 安全性

請參閱

參考

DocumentBase 類別

Microsoft.Office.Tools.Word 命名空間