Attaching custom properties to your VSTO 2005 SmartTag

VSTO 2005 makes attaching custom properties to a Smart Tag easy. These properties can be used by the Smart Tag action. See my previous post VSTO 2005 makes Smart Tags smarter (and easier) to learn how to create VSTO SmartTags. Let’s image that you are creating a VSTO Word document for a large insurance company. You want to create SmartTags for each policy number the user enters. The action for the policy number SmartTag should open a Webpage that has the First Name and Last Name as the arguments for the URL for example https://www.BigInsurance.com/ViewPolicy?FN=Bill;LN=Smith. The problem is that without properties you need to make 2 database calls, one to verify that the policy number exists and the second on the action to lookup the name. SmartTag properties can help you make only one call. When you query the database for the policy number you can pull back and cache in the properties any other values you might need later.With VSTO 2005 this is easy to do. In order to attach the properties you must override the Recognize method. The signature is Protected Overrides Sub Recognize(ByVal text As String, ByVal site As Microsoft.Office.Interop.SmartTag.ISmartTagRecognizerSite, ByVal tokenList As Microsoft.Office.Interop.SmartTag.ISmartTagTokenList) . The two parameters that we will use are “Text” which is a string of what the SmartTag engine has parsed from the document. The second parameter is “site” which is the same ISmartTagRecognizeSite object used by the Office SmartTag SDK (trying to use this SDK will give you a better appreciation of the VSTO SmartTag). Overriding the Recognize event means that we will need to all the SmartTag work ourselves. It’s not too bad though. The 3 things we need to do for our SmartTag are: find a match of the terms we are looking for, attach our properties, and persist the SmartTag. Then we can use the properties on the action event. So let’s look at the code to recognize a simple string from the text that is being passed to the Recognize method. This code finds instances of the string “Bar”. There are many ways to do this. This is just one example that I put together. You could also use regular expressions here or look values up in a database.

        Dim term As String = "Bar"

        'Find if the term exists in the text

        Dim index As Integer = text.IndexOf(term, 0)

        While (index >= 0)

            'I found the term

            'TODO: Add Properties

            'TODO: Persist SmartTag

            'find any more instances of the string

            index += term.Length

            index = text.IndexOf(term, index)

        End While

Now that we have matched our term we need to create a property bag to hold our custom properties. We use the “site” parameter to do this.

'Create a PropertyBag

Dim propertyBag As ISmartTagProperties = site.GetNewPropertyBag()

propertyBag.Write("Key1", "Value1")

propertyBag.Write("Key2", "Value2")

propertyBag.Write("Key3", "Value3")

The last thing to do is to attach the property bag to the SmartTag and attach it to the term in the document. This is done with one line of code.

PersistTag(index, term.Length, propertyBag)

When the action event fires you can retrieve the property bag.

'get the propertyBag off the SmartTag

Dim propertyBag As ISmartTagProperties = e.Properties

MessageBox.Show(propertyBag.Read("Key1"))

Here is the entire code.

Imports Microsoft.Office.Tools.Word

Imports Microsoft.Office.Interop.SmartTag

Public Class SmartTagPropertyBag

    Inherits SmartTag

    'Declare Actions for this SmartTag

    WithEvents Action1 As New Action("Action Number 1")

  WithEvents Action2 As New Action("Action Number 2")

    Public Sub New()

        MyBase.New("https://blogs.msdn.com/pstubbs#PropertyBag", _

        "SmartTag PropertyBag Sample")

        'Add the Actions

        Actions = New Action() {Action1, Action2}

    End Sub

    Protected Overrides Sub Recognize(ByVal text As String, ByVal site