Share via


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

在目標為 .NET Framework 3.5 的 Word 專案中,您可以從 Microsoft.Office.Tools.Word.SmartTag 類別衍生以及覆寫 Recognize方法,藉以控制 Word 如何辨識文件中的智慧標籤。

**適用於:**本主題中的資訊適用於 Word 2007 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

若要執行智慧標籤,使用者必須啟用智慧標籤。 如需詳細資訊,請參閱 HOW TO:在 Word 和 Excel 中啟用智慧標籤

視訊的連結如需觀看本主題的影片版本,請參閱如何:在 Word 中建立智慧標籤?(英文)。 如需觀看相關示範影片,請參閱如何在 Microsoft Word 中使用智慧標籤?(英文)。

若要將智慧標籤及自訂辨識器加入至 Word 文件

  1. 建立新的 Word 2007 文件層級或應用程式層級專案。 如需詳細資訊,請參閱 HOW TO:在 Visual Studio 中建立 Office 專案

  2. 從 [加入參考] 對話方塊的 [.NET] 索引標籤,加入 Microsoft.Office.Interop.SmartTag 組件 (版本 12.0.0.0) 的參考。

  3. 將類別檔案加入專案,並建立繼承自 Microsoft.Office.Tools.Word.SmartTag 的類別。

  4. 在新類別中,建立智慧標籤的動作, 動作是出現在智慧標籤功能表上的項目。 將 Action 型別的執行個體加入至類別的 Actions 集合,藉此建立動作。

  5. 覆寫 SmartTagBase.Recognize 方法,以實作您自訂的辨識行為。

    覆寫 Recognize 方法時,必須呼叫 Microsoft.Office.Tools.Word.SmartTag.PersistTag 方法,才能讓 Word 辨識智慧標籤。

  6. 建立事件處理常式,以回應您所建立動作的 Click 事件和 (選擇性) BeforeCaptionShow 事件。

  7. 在專案文件的程式碼檔案中,將智慧標籤執行個體加入至 ThisDocument 類別 (文件層級專案) 的 VstoSmartTags 屬性或 ThisAddIn 類別 (應用程式層級專案) 的 VstoSmartTags 屬性。

範例

在下列程式碼中,示範了如何在 Word 文件中建立自訂智慧標籤。 這段程式碼會覆寫 Recognize 方法,以辨識 sales 和 organization 這兩個詞彙。 Recognize 方法會將一對索引鍵與值加入至智慧標籤中設為索引鍵的屬性集合中。 然後方法會呼叫 PersistTag 方法,以辨識智慧標籤並儲存新的智慧標籤屬性。

若要測試這段程式碼,請在文件的不同位置輸入文字 sales 和 organization,然後嘗試執行智慧標籤的動作。 其中一個動作會顯示所辨識詞彙的對應屬性值,另一個動作則會顯示智慧標籤的命名空間 (Namespace) 和標題。

Imports Microsoft.Office.Tools.Word
Imports Microsoft.Office.Interop.SmartTag

Public Class CustomSmartTag
    Inherits SmartTag

    ' Declare Actions for this SmartTag
    WithEvents Action1 As New Action("Display property value")
    WithEvents Action2 As New Action("Display smart tag details")

    Public Sub New()
        MyBase.New("https://www.contoso.com/Demo#DemoSmartTag", _
            "Custom Smart Tag")
        Me.Terms.AddRange(New String() {"sales", "organization"})
        Actions = New Action() {Action1, Action2}
    End Sub

    Protected Overrides Sub Recognize(ByVal text As String, _
        ByVal site As ISmartTagRecognizerSite, _
        ByVal tokenList As ISmartTagTokenList)

        ' Determine whether each smart tag term exists in 
        ' the document text.
        Dim Term As String
        For Each Term In Me.Terms

            ' Search the text for the current smart tag term.
            Dim index As Integer = Me.ParagraphText.IndexOf(Term, 0)

            While (index >= 0)

                ' Create a smart tag token and a property bag for the 
                ' recognized term.
                Dim propertyBag As ISmartTagProperties = _
                    site.GetNewPropertyBag()

                ' Write a new property value.
                Dim key As String = "Key1"
                propertyBag.Write(key, DateTime.Now)

                ' Attach the smart tag to the term in the document
                Me.PersistTag(index, Term.Length, propertyBag)

                ' Increment the index and then find the next
                ' instance of the smart tag term.
                index += Term.Length
                index = ParagraphText.IndexOf(Term, index)
            End While
        Next
    End Sub

    ' This action displays the property value for the term.
    Private Sub Action1_Click(ByVal sender As Object, _
        ByVal e As ActionEventArgs) Handles Action1.Click

        Dim propertyBag As ISmartTagProperties = e.Properties
        Dim key As String = "Key1"
        MsgBox("The corresponding value of " & _
            key & " is: " & propertyBag.Read(key))
    End Sub

    ' This action displays smart tag details.
    Private Sub Action2_Click(ByVal sender As Object, _
        ByVal e As ActionEventArgs) Handles Action2.Click

        MsgBox("The current smart tag caption is '" & _
            Me.Caption & "'. The current smart tag type is '" & _
            Me.SmartTagType & "'.")
    End Sub
End Class
using System;
using System.Windows.Forms;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Interop.SmartTag;

public class CustomSmartTag : Microsoft.Office.Tools.Word.SmartTag
{

    // Declare Actions for this SmartTag
    private Microsoft.Office.Tools.Word.Action Action1 =
        new Microsoft.Office.Tools.Word.Action("Display property value");
    private Microsoft.Office.Tools.Word.Action Action2 =
        new Microsoft.Office.Tools.Word.Action("Display smart tag details");

    public CustomSmartTag() : base(
        "https://www.contoso.com/Demo#DemoSmartTag", 
        "Custom Smart Tag")
    {
        this.Terms.AddRange(new string[] { 
            "sales", "organization" });
        Actions = new Microsoft.Office.Tools.Word.Action[] { 
            Action1, Action2 };
        Action1.Click += 
            new ActionClickEventHandler(Action1_Click);
        Action2.Click += 
            new ActionClickEventHandler(Action2_Click);
    }

    protected override void Recognize(string text, 
        ISmartTagRecognizerSite site, ISmartTagTokenList tokenList)
    {
        foreach (string term in this.Terms)
        {
            // Search the text for the current smart tag term.
            int index = this.ParagraphText.IndexOf(term, 0);

            while (index >= 0)
            {
                // Create a smart tag token and a property bag for the 
                // recognized term.
                ISmartTagProperties propertyBag = 
                    site.GetNewPropertyBag();

                // Write a new property value.
                string key = "Key1";
                propertyBag.Write(key, DateTime.Now.ToString());

                // Attach the smart tag to the term in the document
                this.PersistTag(index, term.Length, propertyBag);

                // Increment the index and then find the next
                // instance of the smart tag term.
                index += term.Length;
                index = ParagraphText.IndexOf(term, index);
            }
        }
    }

    // This action displays the property value for the term.
    private void Action1_Click(object sender, 
        Microsoft.Office.Tools.Word.ActionEventArgs e)
    {
        ISmartTagProperties propertyBag = e.Properties;
        string key = "Key1";
        MessageBox.Show("The corresponding value of " + key + 
            " is: " + propertyBag.get_Read(key));
    }

    // This action displays smart tag details.
    private void Action2_Click(object sender, 
        Microsoft.Office.Tools.Word.ActionEventArgs e)
    {
        MessageBox.Show("The current smart tag caption is '" + 
            this.Caption + "'. The current smart tag type is '" + 
            this.SmartTagType + "'.");
    }
}

編譯程式碼

  • 從 [加入參考] 對話方塊的 [COM] 索引標籤,將專案中的參考加入至 [Microsoft Smart Tags 2.0 Type Library]。 請確定參考的 [複製到本機] 屬性為 false。 如果為 true,參考將不會指向正確的主要 Interop 組件,而您就必須從 Microsoft Office 安裝媒體安裝該組件。 如需詳細資訊,請參閱 HOW TO:安裝 Office 主要 Interop 組件

  • 將範例程式碼放入名為 CustomSmartTag 的新類別檔案中。

  • 在 C# 中,變更命名空間,以符合專案名稱。

  • 在類別檔案頂端,加入 Imports (Visual Basic) 或 using (C#) 陳述式,以便使用 Microsoft.Office.Tools.WordMicrosoft.Office.Interop.SmartTag 命名空間。

  • 在專案的 ThisDocument_Startup 或 ThisAddIn_Startup 事件處理常式中加入下列程式碼。 這段程式碼會將自訂智慧標籤加入至文件。

    Me.VstoSmartTags.Add(New CustomSmartTag())
    
    this.VstoSmartTags.Add(new CustomSmartTag());
    

安全性

若要執行智慧標籤,必須啟用 Word 中的智慧標籤。 如需詳細資訊,請參閱 HOW TO:在 Word 和 Excel 中啟用智慧標籤

請參閱

工作

HOW TO:在 Word 和 Excel 中啟用智慧標籤

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

HOW TO:在 Excel 活頁簿中加入智慧標籤

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

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

逐步解說:使用應用程式層級增益集建立智慧標籤

概念

智慧標籤架構

其他資源

智慧標籤概觀