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

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.

Applies to: The information in this topic applies to document-level projects for Word 2007. For more information, see Features Available by Office Application and Project Type.

To run this 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 recognizes a regular expression.

  • 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

Visual Studio opens the new Word document in the designer and adds the My Smart Tag project to Solution Explorer.

Configuring the Project

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

    Imports System.Text.RegularExpressions
    
    using System.Text.RegularExpressions;
    

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

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

    WithEvents action1 As Microsoft.Office.Tools.Word.Action
    
    Private Sub ThisDocument_Startup(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Startup
    
        'Use the following line of code in projects that target the .NET Framework 4.
        Dim smartTag1 As Microsoft.Office.Tools.Word.SmartTag = _
            Globals.Factory.CreateSmartTag( _
           "www.microsoft.com/Demo#DemoSmartTag", _
           "Demonstration Smart Tag")
    
        'In projects that target the .NET Framework 3.5, use the following line of code.
        '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)
    {
        // Use the following line of code in projects that target the .NET Framework 4.
        Microsoft.Office.Tools.Word.SmartTag smartTag1 =
            Globals.Factory.CreateSmartTag(
            "www.microsoft.com/Demo#DemoSmartTag",
            "Demonstration Smart Tag");
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        //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"));
    
  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.

    '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};
    
  3. 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

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.

To create the event handler

  • Copy the following code to the ThisDocument class.

    Private Sub action1_Click(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
        Handles action1.Click
    
        Dim value As String = e.Properties.Read("number")
        Dim fahrenheit As Double = System.Convert.ToDouble(value)
        Dim celsius As Integer = Fix(fahrenheit - 32) * 5 / 9
        e.Range.Text = celsius.ToString() + "C"
    End Sub
    
    void action1_Click(object sender,
        Microsoft.Office.Tools.Word.ActionEventArgs e)
    {
        string value = e.Properties.get_Read("number");
        double fahrenheit = System.Convert.ToDouble(value);
        int celsius = (int)(fahrenheit - 32) * 5 / 9;
        e.Range.Text = celsius.ToString() + "�C";
    }
    

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

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

  4. Click the smart tag icon that appears over the recognized string, and then click Convert to Celsius.

  5. Confirm that the original string is replaced with a new string that contains the temperature in Celsius.

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 Word and .NET Framework 3.5

How to: Create Smart Tags With Custom Recognizers in Excel and .NET Framework 3.5

Walkthrough: Creating a Smart Tag by Using an Application-Level Add-In

Concepts

Smart Tags Architecture

Other Resources

Smart Tags Overview