SmartTag 介面

表示使用 Visual Studio 的 Office 開發工具所自訂之 Word 文件中的智慧標籤。

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

語法

'宣告
<GuidAttribute("2b90a8e2-4e6c-4c27-bd35-a6ba7b344223")> _
Public Interface SmartTag _
    Inherits SmartTagBase
[GuidAttribute("2b90a8e2-4e6c-4c27-bd35-a6ba7b344223")]
public interface SmartTag : SmartTagBase

SmartTag 型別會公開下列成員。

屬性

  名稱 說明
公用屬性 Actions 取得或設定智慧標籤所公開 (Expose) 的動作陣列。 (繼承自 SmartTagBase)。
公用屬性 Caption 取得智慧標籤的名稱。 (繼承自 SmartTagBase)。
公用屬性 DefaultExtension 取得這個 SmartTag 物件的預設副檔名。
公用屬性 Expressions 取得規則運算式的集合,智慧標籤會辨識這些規則運算式。 (繼承自 SmartTagBase)。
公用屬性 Extension 取得這個 SmartTag 物件的自訂副檔名。
公用屬性 SmartTagType 取得做為智慧標籤唯一識別項的命名空間。 (繼承自 SmartTagBase)。
公用屬性 Terms 取得字串常值 (String Literal) 的集合,智慧標籤會辨識這些字串常值。 (繼承自 SmartTagBase)。

回頁首

方法

  名稱 說明
公用方法 Remove 從智慧標籤中移除規則運算式 (Regular Expression) 辨識器。 (繼承自 SmartTagBase)。

回頁首

備註

若要建立智慧標籤,請使用 Globals.Factory.CreateSmartTag 方法建立 SmartTag 物件。 如需詳細資訊,請參閱智慧標籤架構

注意事項注意事項

此介面是由 Visual Studio Tools for Office Runtime 所實作, 並不能實作於您的程式碼中。 如需詳細資訊,請參閱 Visual Studio Tools for Office Runtime 概觀

使用方式

這個型別僅適用於 Word 2007 專案。 智慧標籤在 Word 2010 中已被取代。 如需詳細資訊,請參閱智慧標籤概觀

本文件說明此型別用於以 .NET Framework 4 為目標之 Office 專案的版本。在以 .NET Framework 3.5 為目標的專案中,此型別可能會有不同的成員,而為此型別提供的程式碼範例可能無法運作。 如需此型別在以 .NET Framework 3.5 為目標之專案中的相關文件,請參閱下列 Visual Studio 2008 文件中的參考章節:https://go.microsoft.com/fwlink/?LinkId=160658 (英文)。

範例

下列程式碼範例會示範如何建立智慧標籤。 智慧標籤動作會修改在執行階段時動作的功能表標題,並且顯示所辨認文字的位置。

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Word.SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' For .NET Framework 3.5 projects, use the following code to create the smart tag.
    ' Dim smartTagDemo As New  _
    '     Microsoft.Office.Tools.Word.SmartTag( _
    '     "www.microsoft.com/Demo#DemoSmartTag", _
    '     "Demonstration Smart Tag")

    ' Specify the terms to recognize.
    smartTagDemo.Terms.Add("term")
    smartTagDemo.Terms.Add("recognize")

    ' Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

    ' For .NET Framework 3.5 projects, use the following code to create the action.
    ' displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() { _
            displayAddress}

    ' Add the smart tag.
    Me.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub DisplayAddress_BeforeCaptionShow(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.BeforeCaptionShow

    Dim clickedAction As Microsoft.Office.Tools.Word.Action = _
        TryCast(sender, Microsoft.Office.Tools.Word.Action)

    If clickedAction IsNot Nothing Then
        clickedAction.Caption = "Display the location of " & e.Text
    End If
End Sub

Private Sub DisplayAddress_Click(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.Click

    Dim termStart As Integer = e.Range.Start
    Dim termEnd As Integer = e.Range.End
    MsgBox("The recognized text '" & e.Text & _
            "' begins at position " & termStart & _
            " and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // For .NET Framework 3.5 projects, use the following code to create the smart tag.
    // Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Word.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify the terms to recognize.
    smartTagDemo.Terms.Add("term");
    smartTagDemo.Terms.Add("recognize");

    // Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced"); 

    // For .NET Framework 3.5 projects, use the following code to create the action.
    // displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");

    // Add the action to the smart tag.
    smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { 
        displayAddress };

    // Add the smart tag.
    this.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        displayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        displayAddress_Click);
}

void displayAddress_BeforeCaptionShow(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    Microsoft.Office.Tools.Word.Action clickedAction =
        sender as Microsoft.Office.Tools.Word.Action;

    if (clickedAction != null)
    {
        clickedAction.Caption = "Display the location of " +
            e.Text;
    }
}

void displayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

請參閱

參考

Microsoft.Office.Tools.Word 命名空間

其他資源

智慧標籤架構

HOW TO:將置智慧標籤加入至 Word 文件

HOW TO:使用 Word 中的自訂辨識器和 .NET Framework 3.5 建立智慧標籤

逐步解說:使用文件層級自訂建立智慧標籤