共用方式為


HOW TO:在 Excel 中使用自訂辨識器建立智慧標籤

更新: 2008 年 7 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

文件層級專案

  • Excel 2003

  • Excel 2007

  • Word 2003

  • Word 2007

應用程式層級專案

  • Excel 2007

  • Word 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

您可以從 Microsoft.Office.Tools.Excel.SmartTag 類別 (Class) 衍生以及覆寫 Recognize 方法,控制 Microsoft Office Excel 如何辨識活頁簿中的智慧標籤。

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

若要將智慧標籤及自訂辨識器加入至 Excel 活頁簿

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

    注意事項:

    若要使用應用程式層級專案,您必須安裝 Visual Studio 2008 Service Pack 1 (SP1)。

  2. 從 [加入參考] 對話方塊的 [COM] 索引標籤,將參考加入至 [Microsoft Smart Tags 2.0 Type Library]。

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

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

  5. 覆寫 Recognize 方法,以實作您自訂的辨識行為。實作 Recognize 時,必須呼叫 PersistTag 方法,才能讓 Excel 辨識智慧標籤。

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

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

    注意事項:

    如果您是使用安裝 SP1 之前所建立的應用程式層級專案,則必須修改專案以產生 VstoSmartTags 屬性。如需詳細資訊,請參閱 HOW TO:將應用程式層級智慧標籤加入至在 SP1 前建立的專案中

範例

在下列程式碼中,示範了如何在 Excel 活頁簿中建立自訂智慧標籤。這段程式碼會覆寫 Recognize 方法,以辨識工作表儲存格中的 sales 和 organization 兩個詞彙。Recognize 方法會將一對索引鍵與值加入至智慧標籤中設為索引鍵的屬性集合中。然後方法會呼叫 PersistTag 方法,以辨識智慧標籤並儲存新的智慧標籤屬性。若要測試這段程式碼,請在活頁簿的不同儲存格中輸入文字 sales 和 organization,然後嘗試執行智慧標籤的動作。其中一個動作會顯示所辨識詞彙的對應屬性值,另一個動作則會顯示智慧標籤的命名空間 (Namespace) 和標題。

Imports Microsoft.Office.Tools.Excel
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 cell text for the first instance of 
            ' the current smart tag term.
            Dim index As Integer = Me.CellText.IndexOf(Term, 0)

            If (index >= 0) Then

                ' 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(propertyBag)

                ' This implementation only finds the first instance
                ' of a smart tag term in the cell. 
                Exit For
            End If
        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.Excel;
using Microsoft.Office.Interop.SmartTag;

namespace Trin_ExcelDerivedSmartTags
{
    public class CustomSmartTag : SmartTag {

        // Declare Actions for this SmartTag
        Microsoft.Office.Tools.Excel.Action Action1 =
            new Microsoft.Office.Tools.Excel.Action("Display property value");
        Microsoft.Office.Tools.Excel.Action Action2 =
            new Microsoft.Office.Tools.Excel.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.Excel.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)
        {
            // Determine whether each smart tag term exists in 
            // the document text.
            foreach (string term in this.Terms)
            {
                // Search the cell text for the first instance of 
                // the current smart tag term.
                int index = this.CellText.IndexOf(term, 0);

                if (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(propertyBag);

                    // This implementation only finds the first instance
                    // of a smart tag term in the cell. 
                    break;
                }
            }
        }

        // This action displays the property value for the term.
        private void Action1_Click(object sender, 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, 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 安裝媒體安裝該組件 (Assembly)。如需詳細資訊,請參閱 HOW TO:安裝 Office 主要 Interop 組件

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

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

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

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

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

安全性

您必須在 Excel 中啟用智慧標籤。根據預設,它們都不會啟用。如需詳細資訊,請參閱 HOW TO:在 Word 和 Excel 中啟用智慧標籤

請參閱

工作

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

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

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

HOW TO:在 Word 中使用自訂辨識器建立智慧標籤

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

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

概念

智慧標籤概觀

智慧標籤架構

Office UI 自訂

變更記錄

日期

記錄

原因

2008 年 7 月

加入應用程式層級增益集的新資訊。

SP1 功能變更。