如何:向 Excel 工作簿添加智能标记
更新:2007 年 11 月
适用对象 |
---|
本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 文档级项目
应用程序级项目
有关更多信息,请参见按应用程序和项目类型提供的功能。 |
可以向 Microsoft Office Excel 工作簿中添加智能标记,以便识别文本并使用户能访问与已识别的术语相关的操作。对于文档级项目和应用程序级项目,为创建和配置智能标记而编写的代码都是相同的,但在将智能标记与工作簿关联的方式上存在一些差异。智能标记在文档级项目和应用程序级项目中的作用域也是不同的。
本主题介绍了以下任务:
在文档级自定义项中添加智能标记
在应用程序级外接程序中添加智能标记
若要运行智能标记,最终用户必须在 Word 或 Excel 中启用智能标记。有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记。
在文档级自定义项中添加智能标记
通过使用文档级自定义项添加智能标记时,仅在与该自定义项关联的工作簿中才会识别该智能标记。
通过使用文档级自定义项添加智能标记
创建一个 SmartTag 对象,然后对此对象进行配置以定义智能标记的行为:
若要指定您希望识别的文本,请使用 Terms 或 Expressions 属性。
有关更多信息,请参见智能标记的结构。
将 SmartTag 添加到 ThisWorkbook 类的 VstoSmartTags 属性。
下面的代码示例创建了一个识别单词 sale 和正则表达式 [I|i]ssue\s\d{5,6} 的智能标记。当用户在键入 sale 或与此正则表达式匹配的字符串(如 issue 12345)后单击此智能标记时,它便会显示所识别文本的单元格位置。若要运行此代码,请将此代码添加到 ThisWorkbook 类中,然后从 ThisWorkbook_Startup 事件处理程序中调用 AddSmartTag 方法。
WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action
Private Sub AddSmartTag()
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.
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 OpenMessageBox_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()
{
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.
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);
}
在应用程序级外接程序中添加智能标记
从 SP1 开始,您可以通过使用应用程序级外接程序添加智能标记。您可以指定是要让该智能标记仅在特定工作簿中起作用,还是要让它在所有打开的工作簿中都起作用。在所有打开的工作簿中运行的智能标记称为“应用程序级智能标记”。
向特定工作簿添加智能标记
创建一个 SmartTag 对象,然后对此对象进行配置以定义智能标记的行为:
若要指定您希望识别的文本,请使用 Terms 或 Expressions 属性。
有关更多信息,请参见 智能标记的结构。
使用 GetVstoObject 方法为您希望用来承载此智能标记的工作簿创建一个 Workbook 宿主项。有关创建宿主项的更多信息,请参见在运行时在应用程序级外接程序中扩展 Word 文档和 Excel 工作簿。
说明: 如果您使用的是在安装 SP1 前创建的项目,则必须先修改此项目,然后才能使用 GetVstoObject 方法。有关更多信息,请参见 在运行时在应用程序级外接程序中扩展 Word 文档和 Excel 工作簿。
将 SmartTag 添加到 Workbook 的 VstoSmartTags 属性。
下面的代码示例创建了一个识别单词 sale 和正则表达式 [I|i]ssue\s\d{5,6} 的智能标记。当用户在键入 sale 或与此正则表达式匹配的字符串(如 issue 12345)后单击此智能标记时,它便会显示所识别文本的单元格位置。若要运行此代码,请将此代码添加到 ThisAddIn 类中,然后从 ThisAddIn_Startup 事件处理程序中调用 AddSmartTagToActiveWorkbook 方法。
WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action
Private Sub AddSmartTagToActiveWorkbook()
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.
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 to the active workbook.
Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
Me.Application.ActiveWorkbook.GetVstoObject()
vstoWorkbook.VstoSmartTags.Add(smartTagDemo)
End Sub
Private Sub OpenMessageBox_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 AddSmartTagToActiveWorkbook()
{
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.
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 to the active workbook.
Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
this.Application.ActiveWorkbook.GetVstoObject();
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);
}
添加在所有打开的工作簿中都起作用的智能标记
创建一个 SmartTag 对象,然后对此对象进行配置以定义智能标记的行为:
若要指定您希望识别的文本,请使用 Terms 或 Expressions 属性。
有关更多信息,请参见智能标记的结构。
将 SmartTag 添加到 ThisAddIn 类的 VstoSmartTags 属性。
说明: 如果您使用的是在安装 SP1 前创建的项目,则您必须修改此项目才能生成 VstoSmartTags 属性。有关更多信息,请参见 如何:向在 SP1 之前创建的项目中添加应用程序级智能标记。
下面的代码示例创建了一个识别单词 sale 和正则表达式 [I|i]ssue\s\d{5,6} 的智能标记。当用户在键入 sale 或与此正则表达式匹配的字符串(如 issue 12345)后单击此智能标记时,它便会显示所识别文本的单元格位置。若要运行此代码,请将此代码添加到 ThisAddIn 类中,然后从 ThisAddIn_Startup 事件处理程序中调用 AddSmartTag 方法。
WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action
Private Sub AddSmartTag()
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.
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 OpenMessageBox_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()
{
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.
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 中启用智能标记。
请参见
任务
概念
修订记录
日期 |
修订 |
原因 |
---|---|---|
2008 年 7 月 |
增加了针对应用程序级外接程序的新过程。 |
SP1 功能更改。 |