SmartTag 接口
表示使用 Visual Studio 中的 Office 开发工具自定义的 Excel 工作簿中的智能标记。
命名空间: Microsoft.Office.Tools.Excel
程序集: Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)
语法
声明
<GuidAttribute("f210dc7f-21b5-475e-ae31-76a9b06b9835")> _
Public Interface SmartTag _
Inherits SmartTagBase
[GuidAttribute("f210dc7f-21b5-475e-ae31-76a9b06b9835")]
public interface SmartTag : SmartTagBase
SmartTag 类型公开以下成员。
属性
名称 | 说明 | |
---|---|---|
Actions | 获取或设置一个由智能标记公开的操作的数组。 (继承自 SmartTagBase。) | |
Caption | 获取智能标记的名称。 (继承自 SmartTagBase。) | |
DefaultExtension | 获取此 SmartTag 对象的默认扩展。 | |
Expressions | 获取智能标记将识别的正则表达式的集合。 (继承自 SmartTagBase。) | |
Extension | 获取此 SmartTag 对象的自定义扩展。 | |
SmartTagType | 获取充当智能标记唯一标识符的命名空间。 (继承自 SmartTagBase。) | |
Terms | 获取智能标记将识别的字符串的集合。 (继承自 SmartTagBase。) |
页首
方法
名称 | 说明 | |
---|---|---|
Remove | 从智能标记中移除正则表达式识别器。 (继承自 SmartTagBase。) |
页首
备注
若要创建智能标记,请使用 Globals.Factory.CreateSmartTag 方法来创建 SmartTag 对象。 有关信息,请参见智能标记的结构。
提示
此接口由 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 命名空间