How to: Create Smart Tags With Custom Recognizers in Word and .NET Framework 4
In Word projects that target the .NET Framework 4, you can control how Word recognizes smart tags in documents by implementing the ISmartTagExtension interface.
To run a smart tag, end users must have smart tags enabled in Word or Excel. For more information, see How to: Enable Smart Tags in Word and Excel.
Applies to: The information in this topic applies to document-level projects and application-level projects for Word 2007. For more information, see Features Available by Office Application and Project Type.
To add a smart tag with a custom recognizer to a Word document
Create a document-level or application-level project for Word 2007. For more information, see How to: Create Office Projects in Visual Studio.
Add a reference to the Microsoft.Office.Interop.SmartTag assembly (version 12.0.0.0) from the .NET tab of the Add Reference dialog box.
Add a class file to the project and create a class that implements the ISmartTagExtension interface.
In the new class, create a SmartTag object that represents the smart tag, and create one or more Action objects that represent the smart tag actions. Use the Globals.Factory.CreateSmartTag and Globals.Factory.CreateAction methods to create these objects.
Implement the Recognize method, and write your own custom recognizing behavior.
Your implementation must call the PersistTag method of the context parameter to make Word recognize the smart tag.
Implement the ExtensionBase property to return the SmartTag object.
Create event handlers to respond to the Click event, and optionally the BeforeCaptionShow event, of the actions that you created.
In the code file for the project document, add the smart tag instance to VstoSmartTags property of the ThisDocument class (for a document-level project), or the VstoSmartTags property of the ThisAddIn class (for an application-level project).
Example
The following code example shows how to create a custom smart tag in a Word document. The example implements the Recognize method to recognize the terms sales and organization. The Recognize method adds a key and value pair to the collection of keyed properties for the smart tag. The method then calls the PersistTag method to recognize the smart tag and save the new smart tag property.
To test the example, type the words sales and organization in different places in the document, and then try the smart tag actions. One action displays the corresponding property value for the recognized term, and the other action displays the smart tag namespace and caption.
Imports System
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.SmartTag
Imports Microsoft.Office.Tools.Word
Public Class CustomSmartTag
Implements ISmartTagExtension
' Declare the smart tag.
Private smartTagDemo As Microsoft.Office.Tools.Word.SmartTag
' Declare actions for this smart tag.
WithEvents Action1 As Microsoft.Office.Tools.Word.Action
WithEvents Action2 As Microsoft.Office.Tools.Word.Action
Public Sub New()
Me.smartTagDemo = Globals.Factory.CreateSmartTag(
"https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", Me)
Action1 = Globals.Factory.CreateAction("Display property value")
Action2 = Globals.Factory.CreateAction("Display smart tag details")
smartTagDemo.Terms.AddRange(New String() {"sales", "organization"})
smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() {Action1, Action2}
End Sub
Private Sub Recognize(ByVal text As String,
ByVal site As ISmartTagRecognizerSite,
ByVal tokenList As ISmartTagTokenList,
ByVal context As SmartTagRecognizeContext) Implements ISmartTagExtension.Recognize
For Each term As String In smartTagDemo.Terms
' Search the text for the current smart tag term.
Dim index As Integer = text.IndexOf(term, 0)
While (index >= 0)
' Create a smart tag token and a property bag for the recognized term.
Dim propertyBag As ISmartTagProperties = site.GetNewPropertyBag()
' Write a new property value.
Dim key As String = "Key1"
propertyBag.Write(key, DateTime.Now.ToString())
' Attach the smart tag to the term in the document
context.PersistTag(index, term.Length, propertyBag)
' Increment the index and then find the next instance of the smart tag term.
index += term.Length
index = text.IndexOf(term, index)
End While
Next
End Sub
' This action displays the property value for the term.
Private Sub Action1_Click(ByVal sender As Object,
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles Action1.Click
Dim propertyBag As ISmartTagProperties = e.Properties
Dim key As String = "Key1"
MessageBox.Show(("The corresponding value of " & key & " is: ") + propertyBag.Read(key))
End Sub
' This action displays smart tag details.
Private Sub Action2_Click(ByVal sender As Object,
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles Action2.Click
MessageBox.Show(("The current smart tag caption is '" &
smartTagDemo.Caption & "'. The current smart tag type is '") &
smartTagDemo.SmartTagType & "'.")
End Sub
Public ReadOnly Property Base() As Microsoft.Office.Tools.Word.SmartTag
Get
Return (smartTagDemo)
End Get
End Property
Public ReadOnly Property ExtensionBase() As Object Implements ISmartTagExtension.ExtensionBase
Get
Return (smartTagDemo)
End Get
End Property
End Class
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.SmartTag;
using Microsoft.Office.Tools.Word;
namespace CustomSmartTagExample
{
public class CustomSmartTag : ISmartTagExtension
{
// Declare the smart tag.
Microsoft.Office.Tools.Word.SmartTag smartTagDemo;
// Declare actions for this smart tag.
private Microsoft.Office.Tools.Word.Action Action1;
private Microsoft.Office.Tools.Word.Action Action2;
public CustomSmartTag()
{
this.smartTagDemo = Globals.Factory.CreateSmartTag(
"https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", this);
Action1 = Globals.Factory.CreateAction("Display property value");
Action2 = Globals.Factory.CreateAction("Display smart tag details");
smartTagDemo.Terms.AddRange(new string[] { "sales", "organization" });
smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { Action1, Action2 };
Action1.Click += new ActionClickEventHandler(Action1_Click);
Action2.Click += new ActionClickEventHandler(Action2_Click);
}
void ISmartTagExtension.Recognize(string text, ISmartTagRecognizerSite site, ISmartTagTokenList tokenList,
SmartTagRecognizeContext context)
{
foreach (string term in smartTagDemo.Terms)
{
// Search the text for the current smart tag term.
int index = text.IndexOf(term, 0);
while (index >= 0)
{
// Create a smart tag token and a property bag for the recognized term.
ISmartTagProperties propertyBag = site.GetNewPropertyBag();
// Write a new property value.
string key = "Key1";
propertyBag.Write(key, DateTime.Now.ToString());
// Attach the smart tag to the term in the document
context.PersistTag(index, term.Length, propertyBag);
// Increment the index and then find the next instance of the smart tag term.
index += term.Length;
index = text.IndexOf(term, index);
}
}
}
// This action displays the property value for the term.
private void Action1_Click(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
ISmartTagProperties propertyBag = e.Properties;
string key = "Key1";
MessageBox.Show("The corresponding value of " + key + " is: " + propertyBag.get_Read(key));
}
// This action displays smart tag details.
private void Action2_Click(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
MessageBox.Show("The current smart tag caption is '" +
smartTagDemo.Caption + "'. The current smart tag type is '" + smartTagDemo.SmartTagType + "'.");
}
public Microsoft.Office.Tools.Word.SmartTag Base
{
get { return smartTagDemo; }
}
public object ExtensionBase
{
get { return smartTagDemo; }
}
}
}
Compiling the Code
Add a reference in the project to Microsoft Smart Tags 2.0 Type Library from the COM tab of the Add Reference dialog box. Ensure that the Copy Local property of the reference is false. If it is true, the reference is not to the correct primary interop assembly and you must install the assembly from the Microsoft Office installation media. For more information, see How to: Install Office Primary Interop Assemblies.
Place the example code in a new class file that is named CustomSmartTag.
In C#, change the namespace to match the project name.
Add Imports (in Visual Basic) or using (in C#) statements for the Microsoft.Office.Tools.Word and Microsoft.Office.Interop.SmartTag namespaces at the top of the class file.
Add the following code to the ThisDocument_Startup or ThisAddIn_Startup event handler in your project. This code adds the custom smart tag to the document.
Me.VstoSmartTags.Add(New CustomSmartTag().Base)
this.VstoSmartTags.Add(new CustomSmartTag().Base);
Security
To run the smart tag, smart tags must be enabled in Word. For more information, see How to: Enable Smart Tags in Word and Excel.
See Also
Tasks
How to: Enable Smart Tags in Word and Excel
How to: Add Smart Tags to Word Documents
How to: Add Smart Tags to Excel Workbooks
How to: Create Smart Tags With Custom Recognizers in Excel and .NET Framework 3.5
Walkthrough: Creating a Smart Tag by Using a Document-Level Customization
Walkthrough: Creating a Smart Tag by Using an Application-Level Add-In