更新:2007 年 11 月
| 适用对象 | 
|---|
| 本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 文档级项目 
 应用程序级项目 
 有关更多信息,请参见按应用程序和项目类型提供的功能。 | 
通过从 Microsoft.Office.Tools.Excel.SmartTag 类进行派生并重写 Recognize 方法,可以控制 Microsoft Office Excel 如何识别工作簿中的智能标记。
若要运行智能标记,最终用户必须在 Word 或 Excel 中启用智能标记。有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记。
向 Excel 工作簿添加具有自定义识别器的智能标记
- 为 Excel 创建一个新的文档级项目或应用程序级项目。有关更多信息,请参见如何:创建 Visual Studio Tools for Office 项目。 .gif) 说明: 说明:- 若要使用应用程序级项目,必须安装 Visual Studio 2008 Service Pack 1 (SP1)。 
- 通过“添加引用”对话框的“COM”选项卡添加对“Microsoft Smart Tags 2.0 类型库”的引用。 
- 向项目中添加类文件,并创建一个从 Microsoft.Office.Tools.Excel.SmartTag 继承的类。 
- 在新建的类中,创建智能标记的操作。操作是指显示在智能标记菜单中的项。通过向您的类的 Actions 集合中添加 Action 类型的实例,可以创建操作。 
- 重写 Recognize 方法以实现自己的自定义识别行为。您的 Recognize 实现必须调用 PersistTag 方法以使 Excel 能够识别该智能标记。 
- 创建事件处理程序以响应所创建操作的 Click 事件和 BeforeCaptionShow 事件(后者可选)。 
- 在项目工作簿的代码文件中,将该智能标记实例添加到 ThisWorkbook 类(对于文档级项目)的 VstoSmartTags 属性或 ThisAddIn 类(对于应用程序级项目)的 VstoSmartTags 属性中。 .gif) 说明: 说明:- 如果使用的应用程序级项目是在安装 SP1 之前创建的,则必须修改该项目才能生成 VstoSmartTags 属性。有关更多信息,请参见 如何:向在 SP1 之前创建的项目中添加应用程序级智能标记。 
示例
下面的代码示例演示如何在 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;
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 类型库”的引用。确保该引用的“复制本地”属性为 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 中启用智能标记。
请参见
任务
概念
修订记录
| 日期 | 修订 | 原因 | 
|---|---|---|
| 2008 年 7 月 | 增加了针对应用程序级外接程序的新信息。 | SP1 功能更改。 |