在面向 .NET Framework 3.5 的 Excel 项目中,通过从 Microsoft.Office.Tools.Excel.SmartTag 类派生并重写 Recognize 方法,可以控制 Excel 如何识别文档中的智能标记。
若要运行智能标记,最终用户必须启用智能标记。 有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记。
**适用于:**本主题中的信息适用于 Excel 2007 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能。
向 Excel 工作簿添加具有自定义识别器的智能标记
- 为 Excel 2007 创建一个新的文档级项目或应用程序级项目。 有关更多信息,请参见如何:在 Visual Studio 中创建 Office 项目。 
- 通过**“添加引用”对话框的“.NET”**选项卡添加对 Microsoft.Office.Interop.SmartTag 程序集(12.0.0.0 版)的引用。 
- 向项目中添加类文件,并创建一个从 Microsoft.Office.Tools.Excel.SmartTag 继承的类。 
- 在新建的类中,创建智能标记的操作。 操作是指显示在智能标记菜单中的项。 通过向您的类的 Actions 集合中添加 Action 类型的实例,可以创建操作。 
- 重写 SmartTagBase.Recognize 方法以实现自己的自定义识别行为。 - Recognize 方法的重写必须调用 Microsoft.Office.Tools.Excel.SmartTag.PersistTag 方法才能使 Excel 能够识别智能标记。 
- 创建事件处理程序以响应所创建的操作的 Click 事件和 BeforeCaptionShow 事件(后者可选)。 
- 在项目工作簿的代码文件中,将该智能标记实例添加到 ThisWorkbook 类(对于文档级项目)的 VstoSmartTags 属性或 ThisAddIn 类(对于应用程序级项目)的 VstoSmartTags 属性中。 
示例
下面的代码示例演示如何在 Excel 工作簿中创建自定义智能标记。 该示例重写 Recognize 方法以识别术语 sales 和 organization。 Recognize 方法向智能标记的键属性集合中添加一个键/值对。 该方法随后调用 PersistTag 方法来识别该智能标记,并保存新的智能标记属性。
若要测试该示例,请在工作簿的不同单元格中键入单词 sales 和 organization,然后尝试执行智能标记操作。 其中一项操作显示与所识别的术语相应的属性值,另一项操作显示智能标记命名空间和标题。
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;
    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 类型库”的引用。 确保该引用的“复制本地”**属性为 false。 如果该属性为 true,则表示该引用不是正确的主互操作程序集,您必须从 Microsoft Office 安装媒体中安装该程序集。 有关更多信息,请参见如何:安装 Office 主互操作程序集。 
- 将代码示例放入名为 CustomSmartTag 的新的类文件中。 
- 在 C# 中,将命名空间更改为与项目名称匹配。 
- 在类文件的顶部,为 Microsoft.Office.Tools.Excel 和 Microsoft.Office.Interop.SmartTag 命名空间添加 Imports (Visual Basic) 或 using (C#) 语句。 
- 向项目的 ThisWorkbook_Startup 或 ThisAddIn_Startup 事件处理程序中添加以下代码。 此代码将向工作簿中添加自定义智能标记。 - Me.VstoSmartTags.Add(New CustomSmartTag())- this.VstoSmartTags.Add(new CustomSmartTag());
安全性
必须在 Excel 中启用智能标记。 默认情况下,它们未处于启用状态。 有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记。
请参见
任务
如何:在 Word 和 .NET Framework 3.5 中使用自定义识别器创建智能标记