Smart Tags are VSTO’s newest feature. They have been added to the product since Beta 1 has shipped. VSTO 2005 does for Smart Tags what it did for Action Pane, makes it easy and understandable for a .net programmer. Smart Tags in Office have been around since Office XP and updated in Office 2003. The concept of the SmartTag is cool; it recognizes some text and allows you to attach actions to the text. For example if you have a stock symbol you can recognize the text and attach an action, like open the stock quote web site for that symbol.  Before VSTO there were two basic ways to create a Smart Tag; using XML or creating a DLL.

Using XML allows you to create a simple XML file that defines the Recognizer and the Action to take. Office XP only allows exact text matches, in Office 2003 support was added for Regular Expressions (RegEx). These XML files are called Microsoft Office Smart Tag Lists (MOSTL). MOSTLs are easy to install, just copy them to C:\Program Files\Common Files\Microsoft Shared\Smart Tag\LISTS. The down side to MOSTLs are that the actions are limited to Hyperlinks. They are also limited by passing the entire text that is recognized to the Hyperlink as a {Text} parameter. This is a problem if the text that you want to pass is a substring of the text recognized, for example if your RegEx matches “Bug: 12345” but you only want to pass 12345 to the Hyperlink. Another problem with MOSTLs are that they must be installed on all desktops prior to opening the document. MOSTLs are also global to all applications that support SmartTags and all documents. This forces you to be more conservative with your recognizers because you do not want any false hits. A 5 digit number in one type of document may be a policy number and in another may be a claim number.

The second way to create a SmartTag is to create a DLL (Don’t go there if you have a weak stomach). The SmartTag DLL is the most powerful, and complicated, way to create SmartTags. They are more difficult to install and create. They require you to create a separate Recognizer and Action class that Inherits from 2 different interfaces. The interfaces are not very .Net friendly and are difficult to understand. These SmartTags are also global in scope to the machine just like the MOSTLs, although I believe that you can make them scoped to the document as part of a smart doc solution (I haven’t actually been able to figure this out yet, too painful)

VSTO SmartTags are much easier to use and understand. They offer the simplicity of the MOSTL with the power of the SmartTag DLL. They are scoped to the document they are part of which means that you can be very aggressive about the text you recognize. You can also customize the actions more tightly with the recognized text. Another great feature is that you have access to the range object of the recognized text and access every other part of the VSTO solution including action panes.

So let’s take a look at what it takes to create a VSTO Smart Tag.

1.      Create a New VSTO Word or Excel Project

2.      Add a COM Reference to the Microsoft Smart Tags 2.0 Type Library

3.      Create a Class that Inherits from Microsoft.Office.Tools.Word.SmartTags.SmartTag

4.      Add Actions which are the menu items that come up

5.      Add Recognizers, either RegEx or Text

6.      Add the Event Handlers for the click event of the actions

7.      Add the SmartTag Instance to the documents VSTOSmartTags Collection

That’s it we are done. We now have SmartTag support in our VSTO document.

Here is the Entire Code for the SmartTag Class. This sample recognizes the “Bug 12345” or “Hello”


Imports System.Text.RegularExpressions

Imports Microsoft.Office.Tools.Word

Imports Microsoft.Office.Interop.SmartTag


Public Class MySmartTag

    Inherits SmartTag


    'Declare Actions for the SmartTag

    WithEvents OpenMessageBox As Action

    WithEvents SayHello As Action


    Sub New()

        MyBase.New("", "My First VSTO SmartTag")


        'Menu Items, Text to display on the SmartTag Menu

        OpenMessageBox = New Action("Echo Text to a MessageBox")

        SayHello = New Action("Say Hello")


        'Add the Actions to the SmartTag

        Actions = New Action() {OpenMessageBox, SayHello}

        'Create a RegEx recognizer

        Expressions.Add(New Regex("[B|b]ug\s\d{5,6}"))

        'you can also add exact text matches the same way


    End Sub


    Protected Overrides Sub Recognize(ByVal text As String, ByVal site As Microsoft.Office.Interop.SmartTag.ISmartTagRecognizerSite, ByVal tokenList As Microsoft.Office.Interop.SmartTag.ISmartTagTokenList)

        'Gives you access to the raw tokens as they are being fired

        'Can handle advanced dynamic tag recognizers, like looking

        'text up in a database for a match

    End Sub


    Private Sub OpenMessageBox_Click(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles OpenMessageBox.Click

        'Fire Message Box with the text

        MessageBox.Show("The SmartTag recognized is : " & e.Range.Text)

    End Sub


    Private Sub SayHello_Click(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles SayHello.Click

        'Set the SmarTag Text

        e.Range.Text = "Hello World"

    End Sub


End Class



Public Class ThisDocument

    Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

        'Add the SmartTag to the collection on startup

        VSTOSmartTags.Add(New MySmartTag())

    End Sub


    Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

End Class