This walkthrough demonstrates creating a smart tag that recognizes Fahrenheit temperature strings. The smart tag includes an action that converts the temperature value to Celsius, and replaces the recognized text with a formatted Celsius temperature string.
During this walkthrough, you will learn how to:

Prerequisites
To complete this walkthrough, you will need:

Creating a New Project
In this step, you will create a Word Document project.
To create a new project
Visual Studio opens the new Word document in the designer and adds the My Smart Tag project to Solution Explorer.

Configuring the Project
In this step, you will configure the project to run the code provided in this walkthrough.
To configure your project
-
On the Project menu, click Add Reference.
-
On the COM tab, select Microsoft Smart Tags 2.0 Type Library and click OK.
-
In Solution Explorer, right-click ThisDocument.vb (in Visual Basic) or ThisDocument.cs (in C#) and then click View Code.
-
Add the following line of code to the top of the file:
Imports System.Text.RegularExpressions
using System.Text.RegularExpressions;

Creating the Smart Tag
In this step, you will create a Visual Studio Tools for Office smart tag and add it to the document. You will also add a regular expression to the list of terms that the smart tag recognizes, and create an action that will be available when the user clicks the smart tag.
To create the smart tag
-
Replace the ThisDocument_Startup event handler in the ThisDocument class with the following code. This code creates a SmartTag that represents the Visual Studio Tools for Office smart tag, and adds a regular expression to the list of terms that the smart tag recognizes.
WithEvents action1 As Microsoft.Office.Tools.Word.Action
Private Sub ThisDocument_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
Dim smartTag1 As New Microsoft.Office.Tools.Word.SmartTag( _
"www.microsoft.com/Demo#DemoSmartTag", _
"Demonstration Smart Tag")
smartTag1.Expressions.Add( _
New Regex("(?'number'[+-]?\b[0-9]+)?\s?(F|f)\b"))
private Microsoft.Office.Tools.Word.Action action1;
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Microsoft.Office.Tools.Word.SmartTag smartTag1 =
new Microsoft.Office.Tools.Word.SmartTag(
"www.microsoft.com/Demo#DemoSmartTag",
"Demonstration Smart Tag");
smartTag1.Expressions.Add(new Regex(
@"(?'number'[+-]?\b[0-9]+)?\s?(F|f)\b"));
-
Create a new Action and add it to the Actions property of the smart tag. The Action represents an item that the user can click in the smart tag menu.
action1 = New Microsoft.Office.Tools.Word.Action( _
"Convert to Celsius")
smartTag1.Actions = _
New Microsoft.Office.Tools.Word.Action() {action1}
action1 = new Microsoft.Office.Tools.Word.Action(
"Convert to Celsius");
smartTag1.Actions = new
Microsoft.Office.Tools.Word.Action[] {action1};
-
Attach the smart tag to the document by adding the SmartTag to the VSTOSmartTags property. In C#, attach an event handler to the Click event of the action.
Me.VstoSmartTags.Add(smartTag1)
End Sub
this.VstoSmartTags.Add(smartTag1);
action1.Click += new
Microsoft.Office.Tools.Word.ActionClickEventHandler(
action1_Click);
}

Creating an Event Handler for the Action
In this step, you will add an event handler for the Click event of the action. The event handler retrieves the Fahrenheit temperature value from the key number, which is in the property bag of the smart tag. The event handler then converts the Fahrenheit temperature value to Celsius, and replaces the recognized string. In this example, the key number identifies a captured group from the regular expression assigned to the smart tag. For more information about property bags and regular expressions in Visual Studio Tools for Office smart tags, see Smart Tags Architecture.
To create the event handler

Testing the Application
Now you can test your document to make sure that the smart tag converts Fahrenheit temperatures to Celsius.
To test your workbook
-
Press F5 to run your project.
-
Type a string that conforms to the regular expression you added to the smart tag, such as 60F, 60° F, or 60 F.
Note |
|---|
| To type a degree symbol (°), press ALT and type 248. |
-
Click the smart tag icon that appears over the recognized string and then click Convert to Celsius.
-
Confirm that the original string is replaced with a new string that contains the temperature in Celsius.

See Also