
Adding a Smart Tag by Using an Application-Level Add-In
Starting in SP1, you can add a smart tag by using an application-level add-in. You can specify whether to make the smart tag work only in a specific document, or in all open documents (also called an application-level smart tag).
To add a smart tag to a specific document
Create a SmartTag object, and configure this object to define the behavior of the smart tag:
To specify the text you want to recognize, use the Terms or Expressions properties.
To define the actions that users can click on the smart tag, add one or more Action objects to the Actions property.
For more information, see Smart Tags Architecture.
Use the GetVstoObject method to create a Microsoft.Office.Tools.Word..::.Document host item for the document that will host the smart tag. For more information about creating host items, see Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.
Add the SmartTag to the VstoSmartTags property of the Microsoft.Office.Tools.Word..::.Document.
The following code example creates a smart tag in the active document that recognizes the words term and recognize. When the user clicks the smart tag, it displays the positions of the start and end characters of the recognized word. To run this code, add the code to the ThisAddIn class, call the AddSmartTagToDocument method from the ThisAddIn_Startup event handler, and pass a Microsoft.Office.Interop.Word..::.Document to AddSmartTagToDocument.
Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action
Private Sub AddSmartTagToDocument(ByVal document As Word.Document)
Dim smartTagDemo As New _
Microsoft.Office.Tools.Word.SmartTag( _
"www.microsoft.com/Demo#DemoSmartTag", _
"Demonstration Smart Tag")
' Specify the terms to recognize.
smartTagDemo.Terms.Add("term")
smartTagDemo.Terms.Add("recognize")
' Create the action.
displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")
' Add the action to the smart tag.
smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() { _
displayAddress}
' Get a Document host item, and add the smart tag to the document.
Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
document.GetVstoObject()
vstoDocument.VstoSmartTags.Add(smartTagDemo)
End Sub
Private Sub DisplayAddress_BeforeCaptionShow(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
Handles displayAddress.BeforeCaptionShow
Dim clickedAction As Microsoft.Office.Tools.Word.Action = _
TryCast(sender, Microsoft.Office.Tools.Word.Action)
If clickedAction IsNot Nothing Then
clickedAction.Caption = "Display the location of " & e.Text
End If
End Sub
Private Sub DisplayAddress_Click(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
Handles displayAddress.Click
Dim termStart As Integer = e.Range.Start
Dim termEnd As Integer = e.Range.End
MsgBox("The recognized text '" & e.Text & _
"' begins at position " & termStart & _
" and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;
private void AddSmartTagToDocument(Word.Document document)
{
Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
new Microsoft.Office.Tools.Word.SmartTag(
"www.microsoft.com/Demo#DemoSmartTag",
"Demonstration Smart Tag");
// Specify the terms to recognize.
smartTagDemo.Terms.Add("term");
smartTagDemo.Terms.Add("recognize");
// Create the action.
displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");
// Add the action to the smart tag.
smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] {
displayAddress };
// Add the smart tag to the document.
Microsoft.Office.Tools.Word.Document vstoDocument =
document.GetVstoObject();
vstoDocument.VstoSmartTags.Add(smartTagDemo);
displayAddress.BeforeCaptionShow += new
Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
displayAddress_BeforeCaptionShow);
displayAddress.Click += new
Microsoft.Office.Tools.Word.ActionClickEventHandler(
displayAddress_Click);
}
void displayAddress_BeforeCaptionShow(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
Microsoft.Office.Tools.Word.Action clickedAction =
sender as Microsoft.Office.Tools.Word.Action;
if (clickedAction != null)
{
clickedAction.Caption = "Display the location of " +
e.Text;
}
}
void displayAddress_Click(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
int termStart = e.Range.Start;
int termEnd = e.Range.End;
System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
"' begins at position " + termStart.ToString() +
" and ends at position " + termEnd.ToString());
}
To add a smart tag that works in all open documents
Create a SmartTag object, and configure this object to define the behavior of the smart tag:
To specify the text you want to recognize, use the Terms or Expressions properties.
To define the actions that users can click on the smart tag, add one or more Action objects to the Actions property.
For more information, see Smart Tags Architecture.
Add the SmartTag to the VstoSmartTags property of the ThisAddIn class.
The following code example creates a smart tag that recognizes the words term and recognize. When the user clicks the smart tag, it displays the positions of the start and end characters of the recognized word. To run this code, add the code to the ThisAddIn class, and call the AddSmartTag method from the ThisAddIn_Startup event handler.
Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action
Private Sub AddSmartTag()
Dim smartTagDemo As New _
Microsoft.Office.Tools.Word.SmartTag( _
"www.microsoft.com/Demo#DemoSmartTag", _
"Demonstration Smart Tag")
' Specify the terms to recognize.
smartTagDemo.Terms.Add("term")
smartTagDemo.Terms.Add("recognize")
' Create the action.
displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")
' Add the action to the smart tag.
smartTagDemo.Actions = New Microsoft.Office.Tools.Word.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.Word.ActionEventArgs) _
Handles displayAddress.BeforeCaptionShow
Dim clickedAction As Microsoft.Office.Tools.Word.Action = _
TryCast(sender, Microsoft.Office.Tools.Word.Action)
If clickedAction IsNot Nothing Then
clickedAction.Caption = "Display the location of " & e.Text
End If
End Sub
Private Sub DisplayAddress_Click(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
Handles displayAddress.Click
Dim termStart As Integer = e.Range.Start
Dim termEnd As Integer = e.Range.End
MsgBox("The recognized text '" & e.Text & _
"' begins at position " & termStart & _
" and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;
private void AddSmartTag()
{
Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
new Microsoft.Office.Tools.Word.SmartTag(
"www.microsoft.com/Demo#DemoSmartTag",
"Demonstration Smart Tag");
// Specify the terms to recognize.
smartTagDemo.Terms.Add("term");
smartTagDemo.Terms.Add("recognize");
// Create the action.
displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");
// Add the action to the smart tag.
smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] {
displayAddress };
// Add the smart tag.
this.VstoSmartTags.Add(smartTagDemo);
displayAddress.BeforeCaptionShow += new
Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
displayAddress_BeforeCaptionShow);
displayAddress.Click += new
Microsoft.Office.Tools.Word.ActionClickEventHandler(
displayAddress_Click);
}
void displayAddress_BeforeCaptionShow(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
Microsoft.Office.Tools.Word.Action clickedAction =
sender as Microsoft.Office.Tools.Word.Action;
if (clickedAction != null)
{
clickedAction.Caption = "Display the location of " +
e.Text;
}
}
void displayAddress_Click(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
int termStart = e.Range.Start;
int termEnd = e.Range.End;
System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
"' begins at position " + termStart.ToString() +
" and ends at position " + termEnd.ToString());
}