We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

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:


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

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

  1. On the Project menu, click Add Reference.

  2. On the .NET tab, select Microsoft.Office.Interop.SmartTag and click OK. Select the version of the assembly.

  3. In Solution Explorer, right-click ThisDocument.vb (in Visual Basic) or ThisDocument.cs (in C#), and then click View Code.

  4. Add the following line of code to the top of the file.

    using System.Text.RegularExpressions;

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

  1. 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.

    private Microsoft.Office.Tools.Word.Action RecipeAction;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
        Microsoft.Office.Tools.Word.SmartTag SmartTagDemo =
            new Microsoft.Office.Tools.Word.SmartTag(
            @"Recipe Smart Tag");
        // Specify the terms to recognize.
        SmartTagDemo.Expressions.Add(new Regex(
  2. 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.

    RecipeAction = new Microsoft.Office.Tools.Word.Action(
        @"Convert to ounces");
    // Add the action to the smart tag.
    SmartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { RecipeAction };
  3. 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.

        // Add the smart tag to the document.
        RecipeAction.Click += new Microsoft.Office.Tools.Word.ActionClickEventHandler(

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 void RecipeAction_Click(object sender,
        Microsoft.Office.Tools.Word.ActionEventArgs e)
        string value = e.Properties.get_Read(@"tbsNumber");
        double tbsRecipeAmount = System.Convert.ToDouble(value);
        double ozRecipeAmount = tbsRecipeAmount * 0.5;
        e.Range.InsertAfter(" (" + ozRecipeAmount.ToString() + " oz)");

Now you can test your document to verify that the smart tag converts tablespoon measurements to ounces.

To test your workbook

  1. In Word, enable smart tags.

    For more information, see How to: Enable Smart Tags in Word and Excel.

  2. Press F5 to run your project.

  3. In the Word document, type 1 tablespoon salt.

  4. Click the smart tag icon that appears over 1 tablespoon and then click Convert to ounces.

  5. Confirm that the ounce equivalent is inserted after the tablespoon amount.