In this article

Walkthrough: Creating a Smart Tag by Using a Document-Level Customization

02/04/2013

5 minutes to read

In this article

This walkthrough demonstrates how to create a smart tag in a document-level customization for Word. The smart tag 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.

Creating an action that retrieves data from the smart tag and modifies the recognized smart tag text.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Working with Settings.

Prerequisites

You need the following components to complete this walkthrough:

-

An edition of Visual Studio 2010 that includes the Microsoft Office developer tools. For more information, see [Configuring a Computer to Develop Office Solutions](bb398242\(v=vs.100\).md).

Word 2007.

.NET Framework 3.5.

Note

If you target .NET Framework 4, you must write different code to create smart tags and actions. For more information, see Smart Tags Architecture.

Creating a New Project

The first step is to create a Word Document project.

To create a new project

Create a Word 2007 Document project with the name My Smart Tag, using either Visual Basic or C#. In the wizard, select Create a new document.

Creating the Smart Tag

To enable the smart tag to find and convert Fahrenheit temperature strings, 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 smart tag, and adds a regular expression to the list of terms that the smart tag recognizes.

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.

'Use the following line of code in projects that target the .NET Framework 4.
action1 = Globals.Factory.CreateAction( _
"Convert to Celsius")
'In projects that target the .NET Framework 3.5, use the following line of code.
'action1 = New Microsoft.Office.Tools.Word.Action( _
' "Convert to Celsius")
smartTag1.Actions = _
New Microsoft.Office.Tools.Word.Action() {action1}

// Use the following line of code in projects that target the .NET Framework 4.
action1 = Globals.Factory.CreateAction(
"Convert to Celsius");
// In projects that target the .NET Framework 3.5, use the following line of code.
//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.

Creating an Event Handler for 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 smart tags, see Smart Tags Architecture.