Export (0) Print
Expand All

How to: Create Smart Tags With Custom Recognizers in Word

Note Required applications

The features in this topic are available only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005

    -or-

    Visual Studio Team System

  • Microsoft Office 2003

You can control how Microsoft Office Word recognizes smart tags in Microsoft Visual Studio 2005 Tools for the Microsoft Office System documents by deriving from the Microsoft.Office.Tools.Word.SmartTag class and overriding the Recognize method.

To add a smart tag with a custom recognizer to a Word document

  1. Create a new Word project. For more information, see How to: Create Visual Studio Tools for Office Projects.

  2. Add a reference to Microsoft Smart Tags 2.0 Type Library from the COM tab of the Add Reference dialog box.

  3. Add a class file to the project and create a class that inherits from Microsoft.Office.Tools.Word.SmartTag.

  4. In the new class, create the actions for the smart tags. Actions are items that appear on the smart tags menu. Create actions by adding instances of the Action type to your class's Actions collection.

  5. Override the Recognize method to implement your own custom recognizing behavior. Your implementation of Recognize must call the PersistTag method to make Word recognize the smart tag.

  6. Create event handlers to respond to the Click event, and optionally the BeforeCaptionShow event, of the actions that you created.

  7. In the code file for the project document, add the smart tag instance to VstoSmartTags in the document.

Example

The following code example shows how to create a custom smart tag in a Word document. The example overrides the Recognize method to recognize the terms sales and organization. The Recognize method adds a key and value pair to the collection of keyed properties for the smart tag. The method then calls the PersistTag method to recognize the smart tag and save the new smart tag property. To test the example, type the words sales and organization in different places in the document, and then try the smart tag actions. One action displays the corresponding property value for the recognized term, and the other action displays the smart tag namespace and caption.

using System;
using System.Windows.Forms;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Interop.SmartTag;

public class CustomSmartTag : SmartTag {

    // Declare Actions for this SmartTag
    private Action Action1 = new Action("Display property value");
    private Action Action2 = new Action("Display smart tag details");

    public CustomSmartTag() : base(
        "http://www.contoso.com/Demo#DemoSmartTag", 
        "Custom Smart Tag")
    {
        this.Terms.AddRange(new string[] { 
            "sales", "organization" });
        Actions = new Action[] { Action1, Action2 };
        Action1.Click += 
            new ActionClickEventHandler(Action1_Click);
        Action2.Click += 
            new ActionClickEventHandler(Action2_Click);
    }

    protected override void Recognize(string text, 
        ISmartTagRecognizerSite site, ISmartTagTokenList tokenList)
    {
        foreach (string term in this.Terms)
        {
            // Search the text for the current smart tag term.
            int index = this.ParagraphText.IndexOf(term, 0);

            while (index >= 0)
            {
                // Create a smart tag token and a property bag for the 
                // recognized term.
                ISmartTagProperties propertyBag = 
                    site.GetNewPropertyBag();

                // Write a new property value.
                string key = "Key1";
                propertyBag.Write(key, DateTime.Now.ToString());

                // Attach the smart tag to the term in the document
                this.PersistTag(index, term.Length, propertyBag);

                // Increment the index and then find the next
                // instance of the smart tag term.
                index += term.Length;
                index = ParagraphText.IndexOf(term, index);
            }
        }
    }

    // This action displays the property value for the term.
    private void Action1_Click(object sender, ActionEventArgs e)
    {
        ISmartTagProperties propertyBag = e.Properties;
        string key = "Key1";
        MessageBox.Show("The corresponding value of " + key + 
            " is: " + propertyBag.get_Read(key));
    }

    // This action displays smart tag details.
    private void Action2_Click(object sender, ActionEventArgs e)
    {
        MessageBox.Show("The current smart tag caption is '" + 
            this.Caption + "'. The current smart tag type is '" + 
            this.SmartTagType + "'.");
    }
}

Compiling the Code

  • Add a reference in the project to Microsoft Smart Tags 2.0 Type Library from the COM tab of the Add Reference dialog box. Ensure that the Copy Local property of the reference is false. If it is true, the reference is not to the correct primary interop assembly and you must install the assembly from the Microsoft Office 2003 installation media. For more information, see How to: Install Office Primary Interop Assemblies.

  • Place the example code in a new class file named CustomSmartTag.

  • In C#, change the namespace to match the project name.

  • Add Imports (in Visual Basic) or using (in C#) statements for Microsoft.Office.Tools.Word and Microsoft.Office.Interop.SmartTag at the top of the class file.

  • Add the following code to the Startup event handler in the project document code file. This code adds the custom smart tag to the document.

    Me.VstoSmartTags.Add(New CustomSmartTag())
    

    this.VstoSmartTags.Add(new CustomSmartTag());
    

See Also

Community Additions

ADD
Show:
© 2014 Microsoft