如何:向 Excel 工作簿添加智能标记

可以向 Microsoft Office Excel 工作簿中添加智能标记,以便识别文本并使用户能访问与已识别的术语相关的操作。 对于文档级项目和应用程序级项目,为创建和配置智能标记而编写的代码都是相同的,但在将智能标记与工作簿关联的方式上存在一些差异。 智能标记在文档级项目和应用程序级项目中的作用域也是不同的。

**适用于:**本主题中的信息适用于 Excel 2007 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

本主题介绍了以下任务:

  • 在文档级自定义项中添加智能标记

  • 在应用程序级外接程序中添加智能标记

若要运行智能标记,最终用户必须在 Word 或 Excel 中启用智能标记。 有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记

链接到视频 有关该主题的视频版本,请参见 How Do I: Add Smart Tags to Excel Workbooks(如何实现:向 Excel 工作簿添加智能标记)。

在文档级自定义项中添加智能标记

通过使用文档级自定义项添加智能标记时,仅在与该自定义项关联的工作簿中才会识别该智能标记。

通过使用文档级自定义项添加智能标记

  1. 创建一个 SmartTag 对象,然后对此对象进行配置以定义智能标记的行为:

    • 若要指定您希望识别的文本,请使用 TermsExpressions 属性。

    • 若要定义用户可以在此智能标记上单击的操作,请将一个或多个 Action 对象添加到 Actions 属性。

    有关更多信息,请参见 智能标记的结构

  2. SmartTag 添加到 ThisWorkbook 类的 VstoSmartTags 属性。

下面的代码示例创建了一个识别单词 sale 和正则表达式 [I|i]ssue\s\d{5,6} 的智能标记。 当用户在键入 sale 或与此正则表达式匹配的字符串(如 issue 12345)后单击此智能标记时,它便会显示所识别文本的单元格位置。 若要运行此代码,请将此代码添加到 ThisWorkbook 类中,然后从 ThisWorkbook_Startup 事件处理程序中调用 AddSmartTag 方法。

提示

下面的示例可在以 .NET Framework 4 为目标的项目中运行。 若要在以 .NET Framework 3.5 为目标的项目中使用此示例,请参见代码中的注释。

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Excel.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.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' 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.Excel.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.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.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

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

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

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.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.Excel.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // 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.Excel.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new 
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new 
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

在应用程序级外接程序中添加智能标记

使用应用程序级外接程序添加智能标记时,可以指定是要让该智能标记仅在特定工作簿中起作用,还是要让它在所有打开的工作簿中都起作用。 在所有打开的工作簿中运行的智能标记称为“应用程序级智能标记”。

向特定工作簿添加智能标记

  1. 创建一个 SmartTag 对象,然后对此对象进行配置以定义智能标记的行为:

    • 若要指定您希望识别的文本,请使用 TermsExpressions 属性。

    • 若要定义用户可以在此智能标记上单击的操作,请将一个或多个 Action 对象添加到 Actions 属性。

    有关更多信息,请参见智能标记的结构

  2. 若要为要用于承载智能标记的工作簿创建 Microsoft.Office.Tools.Excel.Workbook 宿主项,请使用 GetVstoObject 方法。 有关创建宿主项的更多信息,请参见在运行时在应用程序级外接程序中扩展 Word 文档和 Excel 工作簿

  3. SmartTag 添加到 Microsoft.Office.Tools.Excel.WorkbookVstoSmartTags 属性。

下面的代码示例创建了一个识别单词 sale 和正则表达式 [I|i]ssue\s\d{5,6} 的智能标记。 当用户在键入 sale 或与此正则表达式匹配的字符串(如 issue 12345)后单击此智能标记时,它便会显示所识别文本的单元格位置。 若要运行此代码,请将此代码添加到 ThisAddIn 类中,接着从 ThisAddIn_Startup 事件处理程序中调用 AddSmartTagToWorkbook 方法,然后将一个 Microsoft.Office.Interop.Excel.Workbook 传递到 AddSmartTagToWorkbook。

提示

下面的示例可在以 .NET Framework 4 为目标的项目中运行。 若要在以 .NET Framework 3.5 为目标的项目中使用此示例,请参见代码中的注释。

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTagToWorkbook(ByVal workbook As Excel.Workbook)
    ' Create a smart tag for .NET Framework 3.5 projects.
    ' Dim smartTagDemo As New  _
    '    Microsoft.Office.Tools.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")
    ' Create a smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' Create the action for .NET Framework 3.5 projects.
    ' displayAddress = New Microsoft.Office.Tools.Excel.Action( _
    '    "To be replaced")
    ' Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

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


    ' Get the host item for the workbook in .NET Framework 3.5 projects.
    ' Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
    '  workbook.GetVstoObject()
    ' Get the host item for the workbook in .NET Framework 4 projects.
    Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
        Globals.Factory.GetVstoObject(workbook)

    ' Add the smart tag to the active workbook.
    vstoWorkbook.VstoSmartTags.Add(smartTagDemo)
End Sub


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

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

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

End Sub

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTagToWorkbook(Excel.Workbook workbook)
{
    Microsoft.Office.Tools.Excel.SmartTag smartTagDemo =
        // Create a smart tag for .NET Framework 3.5 projects.
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //    "www.microsoft.com/Demo#DemoSmartTag",
        //    "Demonstration Smart Tag");
        // Create a smart tag for .NET Framework 4 projects.
        Globals.Factory.CreateSmartTag(
            "www.microsoft.com/Demo#DemoSmartTag",
            "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // Create the action for .NET Framework 3.5 projects.
    // displayAddress = new Microsoft.Office.Tools.Excel.Action(
    //    "To be replaced");
    // Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced");

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

    // Get the host item for the workbook in .NET Framework 3.5 projects.
    // Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
    //    workbook.GetVstoObject();
    // Get the host item for the workbook in .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
        Globals.Factory.GetVstoObject(workbook);

    // Add the smart tag to the active workbook.
    vstoWorkbook.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender,
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

添加在所有打开的工作簿中都起作用的智能标记

  1. 创建一个 SmartTag 对象,然后对此对象进行配置以定义智能标记的行为:

    • 若要指定您希望识别的文本,请使用 TermsExpressions 属性。

    • 若要定义用户可以在此智能标记上单击的操作,请将一个或多个 Action 对象添加到 Actions 属性。

    有关更多信息,请参见 智能标记的结构

  2. SmartTag 添加到 ThisAddIn 类的 VstoSmartTags 属性。

下面的代码示例创建了一个识别单词 sale 和正则表达式 [I|i]ssue\s\d{5,6} 的智能标记。 当用户在键入 sale 或与此正则表达式匹配的字符串(如 issue 12345)后单击此智能标记时,它便会显示所识别文本的单元格位置。 若要运行此代码,请将此代码添加到 ThisAddIn 类中,然后从 ThisAddIn_Startup 事件处理程序中调用 AddSmartTag 方法。

提示

下面的示例可在以 .NET Framework 4 为目标的项目中运行。 若要在以 .NET Framework 3.5 为目标的项目中使用此示例,请参见代码中的注释。

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Excel.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.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' 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.Excel.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.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.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

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

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

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.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.Excel.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // 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.Excel.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new 
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new 
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

安全性

必须在 Excel 中启用智能标记。 默认情况下,它们未处于启用状态。 有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记

请参见

任务

如何:在 Word 和 Excel 中启用智能标记

如何:向 Word 文档添加智能标记

如何:在 Word 和 .NET Framework 3.5 中使用自定义识别器创建智能标记

如何:在 Excel 和 .NET Framework 3.5 中使用自定义识别器创建智能标记

演练:使用文档级自定义项创建智能标记

演练:使用应用程序级外接程序创建智能标记

概念

智能标记的结构

其他资源

How Do I: Add Smart Tags to Excel Workbooks?

智能标记概述

开发 Office 解决方案