Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

Updated: July 2008

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Word 2007

For more information, see Features Available by Application and Project Type.

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can 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 2007 document, and provides an action that converts the amount to ounces. It adds the equivalent ounce amount in parentheses after the tablespoon amount.

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.

NoteNote:

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 Visual Studio Settings.

You need the following components to complete this walkthrough:

  • Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).

  • Microsoft Office Word 2007.

Visual Studio Tools for Office is installed by default with the listed versions of Visual Studio. To check whether it is installed, see Installing Visual Studio Tools for Office.

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 COM tab, select Microsoft Smart Tags 2.0 Type Library and click OK.

  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 Visual Studio Tools for Office 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(
            @"www.microsoft.com/Demo#DemoSmartTag",
            @"Recipe Smart Tag");
    
        // Specify the terms to recognize.
        SmartTagDemo.Expressions.Add(new Regex(
            @"(?'tbsNumber'[+-]?\b[0-9]+)?\s?(tbs|tablespoons|tablespoon)\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.

    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. 
        this.VstoSmartTags.Add(SmartTagDemo);
    
        RecipeAction.Click += new
            Microsoft.Office.Tools.Word.ActionClickEventHandler(
            RecipeAction_Click);
    }
    

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 Visual Studio Tools for Office 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. Type a recipe with ingredient amounts measured in tablespoons.

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

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

Date

History

Reason

July 2008

Added topic.

SP1 feature change.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft