Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Smart Tags Overview
 How to: Add Smart Tags to Excel Wor...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Add Smart Tags to Excel Workbooks

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

  • Document-level projects

Microsoft Office version

  • Excel 2007

  • Excel 2003

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

You can add smart tags to your document-level projects for Microsoft Office Excel to recognize text in a workbook and give the user access to actions related to the recognized terms.

To add smart tags to an Excel workbook

  1. Create a new Excel workbook or template 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.

The following code example shows how to create a smart tag that recognizes the word sale and the regular expression [I|i]ssue\s\d{5,6}. When you run the project, the recognizer marks the word sale and any string that matches the regular expression in the workbook as smart tags. To test the example, type the strings sale and issue 12345 in different cells in the workbook, and then try the smart tag action. The action modifies the menu caption of the action and displays the cell location of the text that was recognized.

Visual Basic
#Region "Smart Tag Example"

WithEvents DisplayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()
    Dim SmartTagDemo As New _
        Microsoft.Office.Tools.Excel.SmartTag( _
        "www.microsoft.com/Demo#DemoSmartTag", _
        "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    SmartTagDemo.Terms.Add("sale")
    SmartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' Create the action.
    DisplayAddress = New _
        Microsoft.Office.Tools.Excel.Action( _
        "To be replaced")

    ' Add the action to the smart tag.
    SmartTagDemo.Actions = New _
        Microsoft.Office.Tools.Excel.Action() { _
            DisplayAddress}

    ' Add the smart tag to the workbook.
    Me.VstoSmartTags.Add(SmartTagDemo)
End Sub

Private Sub OpenMessageBox_BeforeCaptionShow(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

    DisplayAddress.Caption = "Display the address of " & _
            e.Text
End Sub

Private Sub DisplayAddress_Click(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) _
    Handles DisplayAddress.Click

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub

#End Region

C#
#region Smart Tag Example

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

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

    // Specify a term and an expression to recognize.
    SmartTagDemo.Terms.Add("sale");
    SmartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

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

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

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

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

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

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

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

#endregion

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

You must enable smart tags in Excel. By default, they are not enabled. For more information, see Smart Tags Overview.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker