Export (0) Print
Expand All
1 out of 9 rated this helpful - Rate this topic

How to: Add Smart Tags to Word Documents

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 add smart tags to your Microsoft Visual Studio 2005 Tools for the Microsoft Office System projects to recognize text in a document and give the user access to actions related to the recognized terms.

To add smart tags to a Word document

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

  2. Create an instance of the SmartTag class.

  3. Specify text that you want to recognize by passing one or more strings to the Terms property or one or more regular expressions to the Expressions property of the SmartTag.

  4. 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 the collection of actions exposed by the Actions property.

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

  6. In the code file for the project document, add the SmartTag to the collection of smart tags exposed by the VstoSmartTags property.

Example

The following code example shows how to create a smart tag that recognizes the words term and recognize. When you run the project, the recognizer marks the words term and recognize in the document as smart tags. To test the example, type the words term and recognize in different places in the document, and then try the smart tag action. The action modifies the menu caption of the action and displays the location of the text that was recognized.

#region Smart Tag Example

private Microsoft.Office.Tools.Word.Action DisplayAddress;

private void AddSmartTag()
{
    Microsoft.Office.Tools.Word.SmartTag SmartTagDemo =
        new Microsoft.Office.Tools.Word.SmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // Specify the terms to recognize.
    SmartTagDemo.Terms.Add("term");
    SmartTagDemo.Terms.Add("recognize");

    // Create the action.
    DisplayAddress = new Microsoft.Office.Tools.Word.Action(
        "To be replaced");

    // Add the action to the smart tag.
    SmartTagDemo.Actions = new
        Microsoft.Office.Tools.Word.Action[] { 
        DisplayAddress };

    // Add the smart tag to the document.
    this.VstoSmartTags.Add(SmartTagDemo);

    DisplayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    DisplayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        DisplayAddress_Click);
}

void DisplayAddress_BeforeCaptionShow(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    DisplayAddress.Caption = "Display the location of " +
        e.Text;
}

void DisplayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

#endregion

Compiling the Code

  • Add the code to your project and call the AddSmartTag method from the Startup event handler in the project document code file.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.