Walkthrough: Creating a Smart Tag by Using an Application-Level Add-In
This walkthrough demonstrates how to create an application-level smart tag that you can use in every open document. The smart tag recognizes tablespoon measurements in a Microsoft Office Word document, and provides an action that converts the amount to ounces. It adds the equivalent ounce amount in parentheses after the tablespoon amount.
Applies to: The information in this topic applies to application-level projects for Word 2007. For more information, see Features Available by Office Application and Project Type.
To run the smart tag, end users must enable smart tags in Word. For more information, see How to: Enable Smart Tags in Word and Excel.
This walkthrough illustrates the following tasks:
Creating a smart tag that uses a regular expression to recognize strings.
Creating an action that retrieves data from the smart tag and modifies the recognized smart tag text.
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.
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.
.NET Framework 3.5.
If you target .NET Framework 4, you must write different code to create smart tags and actions. For more information, see Smart Tags Architecture.
The first step is to create a Word add-in project.
To create a new project
Create a Word 2007 Add-in project with the name My Recipe Smart Tag, using either Visual Basic or C#.
For more information, see How to: Create Office Projects in Visual Studio.
Visual Studio adds the My Recipe Smart Tag project to Solution Explorer.
The project needs a reference to the smart tag DLL, and also needs to use regular expressions.
To configure your project
On the Project menu, click Add Reference.
On the .NET tab, select Microsoft.Office.Interop.SmartTag and click OK. Select the 126.96.36.199 version of the assembly.
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.
To enable the smart tag to find and convert tablespoon measurements, 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 ThisAddIn_Startup event handler in the ThisAddIn 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.
WithEvents RecipeAction As Microsoft.Office.Tools.Word.Action Private Sub ThisAddIn_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup Dim SmartTagDemo As New Microsoft.Office.Tools.Word.SmartTag( _ "www.microsoft.com/Demo#DemoSmartTag", "Recipe Smart Tag") SmartTagDemo.Expressions.Add(New Regex( _ "(?'tbsNumber'[+-]?\b[0-9]+)?\s?(tbs|tablespoons|tablespoon)\b"))
Attach the smart tag to the VstoSmartTags property of the ThisAddIn class. In C#, attach an event handler to the Click event of the action.
The event handler retrieves the tablespoon value from the key tbsNumber, which is in the property bag of the smart tag. The event handler then converts the tablespoon amount to ounces, and inserts the ounce value in parentheses after the tablespoon value.
In this example, the key tbsNumber 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.
To create the event handler
Copy the following code to the ThisAddIn class.
Private Sub RecipeAction_Click(ByVal sender As Object, _ ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _ Handles RecipeAction.Click Dim value As String = e.Properties.Read("tbsNumber") Dim tbsRecipeAmount As Double = System.Convert.ToDouble(value) Dim ozRecipeAmount As Double = tbsRecipeAmount * 0.5 e.Range.InsertAfter(" (" + ozRecipeAmount.ToString() + " oz)") End Sub
Now you can test your document to verify that the smart tag converts tablespoon measurements to ounces.
To test your workbook
In Word, enable smart tags.
For more information, see How to: Enable Smart Tags in Word and Excel.
Press F5 to run your project.
In the Word document, type 1 tablespoon salt.
Click the smart tag icon that appears over 1 tablespoon and then click Convert to ounces.
Confirm that the ounce equivalent is inserted after the tablespoon amount.