HOW TO:在 Excel 活頁簿中加入智慧標籤
您可以將智慧標籤加入至 Microsoft Office Excel 活頁簿以識別文字,並讓使用者存取與所識別之詞彙相關的動作。 為文件層級專案和應用程式層級專案建立及設定智慧標籤,所撰寫的程式碼相同,但對於將智慧標籤與活頁簿產生關聯的方式則有些差異。 在文件層級專案與應用程式層級專案中,智慧標籤的範圍也不同。
**適用於:**本主題中的資訊適用於 Excel 2007 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
本主題將說明下列工作:
在文件層級自訂中加入智慧標籤
在應用程式層級增益集中加入智慧標籤
若要執行智慧標籤,使用者必須在 Word 或 Excel 中啟用智慧標籤。 如需詳細資訊,請參閱 HOW TO:在 Word 和 Excel 中啟用智慧標籤。
如需觀看本主題的影片版本,請參閱如何:將智慧標籤加入至 Excel 活頁簿? (英文)。
在文件層級自訂中加入智慧標籤
使用文件層級自訂加入智慧標籤時,只有在與自訂相關聯的活頁簿中,才會辨識智慧標籤。
若要使用文件層級自訂加入智慧標籤
建立 SmartTag 物件並設定此物件,以定義智慧標籤的行為:
若要指定您想要辨認的文字,請使用 Terms 或 Expressions 屬性。
如需詳細資訊,請參閱 智慧標籤架構。
將 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);
}
在應用程式層級增益集中加入智慧標籤
當您使用應用程式層級增益集加入智慧標籤時,可以指定智慧標籤只適用於特定活頁簿,還是適用於所有開啟的活頁簿。 在所有開啟的活頁簿中執行的智慧標籤稱為「應用程式層級智慧標籤」(Application-Level Smart Tag)。
若要將智慧標籤加入至特定活頁簿
建立 SmartTag 物件並設定此物件,以定義智慧標籤的行為:
若要指定您想要辨認的文字,請使用 Terms 或 Expressions 屬性。
如需詳細資訊,請參閱 智慧標籤架構。
若要為要裝載智慧標籤的活頁簿建立 Microsoft.Office.Tools.Excel.Workbook 主項目,請使用 GetVstoObject 方法。 如需建立主項目的詳細資訊,請參閱在應用程式層級增益集的執行階段中擴充 Word 文件和 Excel 活頁簿。
將 SmartTag 加入至 Microsoft.Office.Tools.Excel.Workbook 的 VstoSmartTags 屬性。
下列程式碼範例會建立智慧標籤,以辨認 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);
}
若要加入可用於所有開啟中活頁簿的智慧標籤
建立 SmartTag 物件並設定此物件,以定義智慧標籤的行為:
若要指定您想要辨認的文字,請使用 Terms 或 Expressions 屬性。
如需詳細資訊,請參閱 智慧標籤架構。
將 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 中的智慧標籤。 根據預設,它們都不會啟用。 如需詳細資訊,請參閱 HOW TO:在 Word 和 Excel 中啟用智慧標籤。
請參閱
工作
HOW TO:使用 Word 中的自訂辨識器和 .NET Framework 3.5 建立智慧標籤
HOW TO:在 Excel 和 .NET Framework 3.5 中使用自訂辨識器建立智慧標籤