Action 接口

表示使用 Visual Studio 中的 Office 开发工具自定义的 Excel 工作簿中的智能标记操作。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)

语法

声明
<GuidAttribute("37e25b46-6941-4833-9b08-e692cf461982")> _
Public Interface Action _
    Inherits ActionBase
[GuidAttribute("37e25b46-6941-4833-9b08-e692cf461982")]
public interface Action : ActionBase

Action 类型公开以下成员。

属性

  名称 说明
公共属性 Caption 获取或设置操作的名称,如智能标记菜单中所示。 (继承自 ActionBase。)

页首

事件

  名称 说明
公共事件 BeforeCaptionShow 在用户单击智能标记图标之后、智能标记菜单显示之前发生。
公共事件 Click 在智能标记菜单中的操作被单击时发生。

页首

备注

在识别到某种类型的智能标记时,智能标记的快捷菜单上会出现可供选择的操作。 若要创建操作,请使用 Globals.Factory.CreateAction 方法来创建 Action 对象。 有关更多信息,请参见 智能标记的结构

提示

此接口由 Visual Studio Tools for Office 运行时实现。不应在代码中实现此接口。有关更多信息,请参见 Visual Studio Tools for Office Runtime 概述

用法

此类型的设计目的是仅用在 Excel 2007 的项目中。 智能标记在 Excel 2010 中被弃用。 有关更多信息,请参见 智能标记概述

本文档介绍面向 .NET Framework 4 的 Office 项目中所用此类型的版本。在面向 .NET Framework 3.5 的项目中,此类型可能具有不同的成员,因此本文档为此类型提供的代码示例可能并不适用。有关在面向 .NET Framework 3.5 的项目中使用此类型的文档,请参见 Visual Studio 2008 文档中以下参考部分:https://go.microsoft.com/fwlink/?LinkId=160658

示例

下面的代码示例创建一个 SmartTag,该智能标记有一个可识别术语“sale”和正则表达式“[I|i]ssue\s\d{5,6}”的 Action。 该操作在运行时修改操作的菜单标题,并显示已识别的文本所在的地址。 若要测试该示例,请在一个单元格中键入单词“sale”,在另一个单元格中键入字符串“issue 12345”,然后尝试执行该智能标记操作。

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);
}

请参见

参考

Microsoft.Office.Tools.Excel 命名空间

SmartTag

其他资源

智能标记的结构

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

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