Microsoft KB Archive/306058

= How to create an Office XP Smart Tag DLL by using Visual Basic .NET =

Article ID: 306058

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Excel 2002 Standard Edition
 * Microsoft Word 2002 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework Class Libraries 1.0

-



This article was previously published under Q306058





For a Microsoft Visual C# .NET version of this article, see 306422.



For a Microsoft Visual Basic 6.0 version of this article, see 286267.



For a Microsoft ATL version of this article, see 292596.

IN THIS TASK
SUMMARY Smart Tag Interfaces Step-by-Step Instructions to Build a Smart Tag DLL
 * Create the Resource Libraries and Install Them in the Global Assembly Cache
 * Create the Smart Tag DLL
 * Register the Smart Tag DLL for Use with Office
 * Steps to Test the Smart Tag DLL

Troubleshooting REFERENCES



SUMMARY
Smart Tags are a technology that was introduced with Office XP that provides Office users more interactivity with the content of their Office documents. A Smart Tag is an element of text in an Office document that is recognized as having custom actions associated with it. An example of one of these special elements of text might be an e-mail name that is typed into a Word document or an Excel workbook. If the e-mail name is recognized as a Smart Tag, the user is presented with one or more actions to perform on the specified text. Possible actions associated with an e-mail name are to look up additional contact information, or send a new e-mail message to that contact.

You can extend the capabilities of Office XP by developing your own Smart Tag Recognizer/Action DLL for use in Office documents. This article describes how to build a Smart Tag DLL by using Visual Basic .NET and discusses what registry settings are required for Office XP to identify and use your Smart Tag DLL.

NOTE: Excel 2002 and Word 2002 are the only Office XP programs that support Smart Tags. However, the information presented in this article can be applied to Smart Tag development for any program that adopts the Smart Tag technology.

back to the top

Smart Tag interfaces
A Smart Tag DLL is an object that implements two special interfaces: ISmartTagRecognizer and ISmartTagAction. The ISmartTagRecognizer interface is responsible for recognizing text that is typed into a document as a Smart Tag. The ISmartTagAction interface is responsible for performing actions on a particular Smart Tag string at the user's request. It is not required that these interfaces be implemented in the same DLL. You can have a recognizer DLL and one or more action DLLs that extend a single Smart Tag type for different actions.

Documentation for these interfaces, and the type library that defines them, is provided in the Smart Tag Software Development Kit (SDK). If you have not already done so, install the Smart Tag SDK before you continue with the steps to create the sample Smart Tag. You can obtain the Smart Tag SDK from the Microsoft Office XP Developer (MOD) CD-ROM, or you can download it. To download the Smart Tag SDK, visit the following Microsoft Web site:

Office XP Smart Tags SDK Version 1.1

http://www.microsoft.com/downloads/details.aspx?familyid=3d8edcee-0246-43ef-a70f-94c0899f1e8f&displaylang=en

back to the top

Step-by-Step Instructions to Build a Smart Tag DLL
The following steps create a simple Smart Tag DLL that recognizes the Microsoft Network (MSN) Instant Messenger contacts and gives the user the ability to send e-mail or instant messages to a recognized contact. You must have Instant Messenger 7.5 or a later version to use this sample. If you do not have Instant Messenger 7.5 or a later version, you can download a copy. To do this, visit the following Microsoft Web site:

MSN Messenger

http://messenger.msn.com/

back to the top

Create the resource libraries and install them in the Global Assembly Cache
 On the Start menu, point to Programs, click Microsoft Visual Studio .NET, click Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt. Change to the root directory of drive C.  Generate strong named key pairs by issuing the following commands: sn -k SmartTagLib.snk sn -k Messenger.snk sn -k SmartTagSample.snk  From the command prompt, change the directory to the location that contains the Smart Tag SDK type library. The default location for this directory is C:\Program Files\Common Files\Microsoft Shared\Smart Tag.  Issue the following command to generate the strong named wrapper DLL from the type library: tlbimp mstag.tlb /keyfile:c:\SmartTagLib.snk /out:c:\SmartTagLib.dll </li>  Issue the following command to install this DLL into the global assembly cache: <pre class="fixed_text">gacutil /i c:\SmartTagLib.dll </li> From the command prompt, change the directory to the installation location for MSN Instant Messenger. The default location for this directory is C:\Program Files\Messenger.</li>  Issue the following command to generate the strong named wrapper DLL from the type library: <pre class="fixed_text">tlbimp msmsgs.exe\3 /keyfile:c:\Messenger.snk /out:c:\Messenger.dll </li>  Issue the following command to install this DLL into the global assembly cache: <pre class="fixed_text">gacutil /i c:\Messenger.dll </li></ol>

back to the top

Create the Smart Tag DLL
<ol> Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual Basic Projects, and then select Class Library. Name the project SmartTagSample. Class1 is created by default.</li> Add references to the MSN Instant Messenger Library and the Smart Tag SDK library that you just created. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> On the Project menu, click Add Reference.</li> Click Browse. In the Select Component dialog box, select C:\SmartTagLib.dll, and then click Open.</li> Click Browse. In the Select Component dialog box, select C:\Messenger.dll, and then click Open.</li> Click OK in the Add References dialog box to accept your selections.</li></ol> </li> In the Project Properties window, rename Class1.vb to Recognizer.vb.</li>  Replace the code in the code window for Recognizer.vb with the following: Public Class Recognizer Implements SmartTagLib.ISmartTagRecognizer Dim Contacts As Array Public Sub New Dim i As Integer Dim oMessenger As New Messenger.MessengerClass Dim oUsers As Messenger.IMessengerContacts Dim oUser As Messenger.IMessengerContact oUsers = oMessenger.MyContacts Contacts = Array.CreateInstance(GetType(String), oUsers.Count) i = 0 For Each oUser In oUsers Contacts.SetValue(oUser.FriendlyName, i)              i = i + 1 Next End Sub Public ReadOnly Property Desc(ByVal LocaleID As Integer) As String _ Implements SmartTagLib.ISmartTagRecognizer.Desc Get Debug.WriteLine(&quot;Recognizer::Desc&quot;) ' Return a long description of the recognizer. Return &quot;Microsoft Messenger recognizes your Instant Messenger Contacts&quot; End Get End Property Public ReadOnly Property Name(ByVal LocaleID As Integer) As String _ Implements SmartTagLib.ISmartTagRecognizer.Name Get Debug.WriteLine(&quot;Recognizer::Name&quot;) ' Return a short title about the recognizer. Return &quot;Microsoft Messenger Contacts Visual Basic .NET Recognizer&quot; End Get End Property Public ReadOnly Property ProgId As String _ Implements SmartTagLib.ISmartTagRecognizer.ProgId Get Debug.WriteLine(&quot;Recognizer::ProgID&quot;) ' Return the ProgID of the Recognizer interface. Return &quot;SmartTagSample.Recognizer&quot; End Get End Property Public Sub Recognize(ByVal [Text] As String, ByVal DataType As SmartTagLib.IF_TYPE, _ ByVal LocaleID As Integer, ByVal RecognizerSite As SmartTagLib.ISmartTagRecognizerSite) _ Implements SmartTagLib.ISmartTagRecognizer.Recognize Debug.WriteLine(&quot;Recognizer::Recognize&quot;) ' The Recognize method is called and passed a text value. ' You should recognize strings in the text and set up the actions. Dim i As Integer Dim startpos As Integer Dim strlen As Integer Dim propbag As SmartTagLib.ISmartTagProperties ' Loop through all contacts. For i = 0 To Contacts.GetUpperBound(0) ' Look for a contact name in the text. startpos = Text.IndexOf(Contacts(i)) + 1 ' Find the length of the contact name. strlen = Contacts(i).Length ' Look for all occurrences of contacts in the string. While (startpos > 0) ' Create a new property bag. propbag = RecognizerSite.GetNewPropertyBag ' Commit the Smart Tag to the property bag. RecognizerSite.CommitSmartTag(&quot;microsoft/messenger#contacts&quot;, _                           startpos, strlen, propbag) ' Continue looking for contacts in the string. startpos = Text.IndexOf(Contacts(i), startpos + strlen) + 1 End While Next i      End Sub Public ReadOnly Property SmartTagCount As Integer _ Implements SmartTagLib.ISmartTagRecognizer.SmartTagCount Get Debug.WriteLine(&quot;Recognizer::SmartTagCount&quot;) ' Return the number of Smart Tags that you support. Return 1 End Get End Property Public ReadOnly Property SmartTagDownloadURL(ByVal SmartTagID As Integer) As String _ Implements SmartTagLib.ISmartTagRecognizer.SmartTagDownloadURL Get Debug.WriteLine(&quot;Recognizer::SmartTagDownloadURL&quot;) ' Return the URL that is embedded in documents. Return &quot;&quot; End Get End Property Public ReadOnly Property SmartTagName(ByVal SmartTagID As Integer) As String _ Implements SmartTagLib.ISmartTagRecognizer.SmartTagName Get Debug.WriteLine(&quot;Recognizer::SmartTagName&quot;) ' This method is called the same number of times as you ' return in SmartTagCount. This method sets a unique name ' for the Smart Tag. Return &quot;microsoft/messenger#contacts&quot; End Get End Property End Class </li> On the Project menu, select Add Class. Under Templates, select Class, and name the class Action.vb. Click Open to create the new class.</li>  Replace the code in the code window for Action.vb with the following: Public Class Action Implements SmartTagLib.ISmartTagAction Public ReadOnly Property Desc(ByVal LocaleID As Integer) As String _ Implements SmartTagLib.ISmartTagAction.Desc Get Debug.WriteLine(&quot;Action::Desc&quot;) ' Return a long description describing the action. Return &quot;Provides actions for the Messenger Smart Tag&quot; End Get End Property Public Sub InvokeVerb(ByVal VerbID As Integer, ByVal _ ApplicationName As String, ByVal Target As Object, ByVal Properties _ As SmartTagLib.ISmartTagProperties, ByVal [Text] As String, ByVal _ Xml As String) Implements SmartTagLib.ISmartTagAction.InvokeVerb Debug.WriteLine(&quot;Action::InvokeVerb&quot;) ' This method is called when a user invokes a verb ' from the Smart Tag menu. Dim oMessenger As New Messenger.MessengerClass Dim oUsers As Messenger.IMessengerContacts Dim oUser As Messenger.IMessengerContact oUsers = oMessenger.MyContacts ' Loop through the contacts. For Each oUser In oUsers ' Verify that the contact is the correct one. If (String.Compare(oUser.FriendlyName, Text, True) = 0) Then Select Case VerbID Case 1 ' The user wants to display the Instant Message ' box to send the contact a message. oMessenger.InstantMessage(oUser) Case 2 ' Shell the &quot;mailto&quot; protocol to start the ' mail program and create a new message. System.Diagnostics.Process.Start(&quot;mailto:&quot; & _ oUser.EmailAddress) End Select End If          Next End Sub Public ReadOnly Property Name(ByVal LocaleID As Integer) As String _ Implements SmartTagLib.ISmartTagAction.Name Get Debug.WriteLine(&quot;Action::Name&quot;) ' Return a short name describing the Action. Return &quot;Messenger Smart Tag&quot; End Get End Property Public ReadOnly Property ProgId As String _ Implements SmartTagLib.ISmartTagAction.ProgId Get Debug.WriteLine(&quot;Action::ProgID&quot;) ' Return the ProgID of the Action interface. Return &quot;SmartTagSample.Action&quot; End Get End Property Public ReadOnly Property SmartTagCaption(ByVal SmartTagID As _ Integer, ByVal LocaleID As Integer) As String _ Implements SmartTagLib.ISmartTagAction.SmartTagCaption Get Debug.WriteLine(&quot;Action::SmartTagCaption&quot;) ' This caption appears on the menu for the smart tag. Return &quot;Messenger Smart Tag&quot; End Get End Property Public ReadOnly Property SmartTagCount As Integer _ Implements SmartTagLib.ISmartTagAction.SmartTagCount Get Debug.WriteLine(&quot;Action::SmartTagCount&quot;) ' Return the number of smart tags this action supports. Return 1 End Get End Property Public ReadOnly Property SmartTagName(ByVal SmartTagID As _ Integer) As String Implements SmartTagLib.ISmartTagAction.SmartTagName Get Debug.WriteLine(&quot;Action::SmartTagName&quot;) ' This method is called the same number of times as you ' return in SmartTagCount. This method sets a unique name ' for the smart tag. Return &quot;microsoft/messenger#contacts&quot; End Get End Property Public ReadOnly Property VerbCaptionFromID(ByVal VerbID As _ Integer, ByVal ApplicationName As String, ByVal LocaleID As Integer) _ As String Implements SmartTagLib.ISmartTagAction.VerbCaptionFromID Get Debug.WriteLine(&quot;Action::VerbCaptionFromID&quot;) ' Get a caption for each verb. This caption appears ' on the Smart Tag menu. Select Case VerbID Case 1 Return &quot;Send this contact an Instant Message&quot; Case 2 Return &quot;Send e-mail to this contact&quot; End Select End Get End Property Public ReadOnly Property VerbCount(ByVal SmartTagName As String) _ As Integer Implements SmartTagLib.ISmartTagAction.VerbCount Get Debug.WriteLine(&quot;Action::VerbCount&quot;) ' Return the number of verbs you support. If (SmartTagName.Equals(&quot;microsoft/messenger#contacts&quot;)) Then Return 2 End If          End Get End Property Public ReadOnly Property VerbID(ByVal SmartTagName As String, _ ByVal VerbIndex As Integer) As Integer Implements SmartTagLib.ISmartTagAction.VerbID Get Debug.WriteLine(&quot;Action::VerbID&quot;) ' Return a unique ID for each verb you support. Return VerbIndex End Get End Property Public ReadOnly Property VerbNameFromID(ByVal VerbID As Integer) As String _ Implements SmartTagLib.ISmartTagAction.VerbNameFromID Get ' Return a string name for each verb. Select Case VerbID Case 1 Return &quot;SendInstantMessage&quot; Case 2 Return &quot;SendEmail&quot; End Select End Get End Property End Class </li>  Add the following line to the end of AssemblyInfo.vb: <Assembly: AssemblyKeyFile(&quot;C:\SmartTagSample.snk&quot;)> </li> On the Build menu, click Build Solution to create the DLL.</li> <li>From a Visual Studio .NET command prompt, change the directory to the bin directory for your project.</li> <li> For this DLL to work correctly, you must install it into the global assembly cache. Issue the following command to install the DLL: <pre class="fixed_text">gacutil /i SmartTagSample.dll </li> <li> To register the .NET DLL so that COM clients can invoke it, run the following command: <pre class="fixed_text">regasm SmartTagSample.dll </li></ol>

back to the top

Register the Smart Tag DLL for use with Office
Warning Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems might require that you reinstall your operating system. Microsoft cannot guarantee that these problems can be solved. Modify the registry at your own risk.

Before you can use any Smart Tag DLL, it must be registered on the system. Typical COM registration is completed for you when you compile the project, distribute the DLL with the Package and Deployment Wizard, or call Regsvr32.exe with the DLL name. You must create additional registry entries that are not part of typical COM registration so that Office applications can identify the DLL as a Smart Tag DLL. To do this, follow these steps: <ol> <li>From a command line, start Regedit.exe.</li> <li>Locate the following key in the registry:

HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Actions

</li> <li>Add a new subkey named SmartTagSample.Action.</li> <li>Locate the following key in the registry:

HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Recognizers

</li> <li>Add a new subkey named SmartTagSample.Recognizer.</li> <li>Close Registry Editor.</li></ol>

back to the top

Steps to test the Smart Tag DLL
Smart Tags obey the same security model as macros. If the security settings of the application are set to High, the Smart Tag DLL does not load unless Trust all installed add-ins and templates is selected. If the security settings are set to Medium and Trust all installed add-ins and templates is not selected, the user is prompted to enable or disable each add-in. Because the InprocServer32 key for the .NET assembly references the Mscoree.dll file instead of the DLL that you build, a dialog box prompts you to enable or disable macros for Mscoree.dll.

To test the custom Smart Tag Recognizer/Action DLL in Word, follow these steps:
 * 1) Start Instant Messenger, and log on.

NOTE: The sample Smart Tag requires that you log on to Instant Messenger; if you are not logged on to Instant Messenger, the custom DLL loads but does not recognize any contacts.
 * 1) Start Word 2002. On the Tools menu, point to Macro, and then click Security. Set the macro security to Medium, and then click OK. If the macro security setting was previously set to High, restart Word.
 * 2) Type the friendly name of a contact in a new document (for example, John Smith), and then press ENTER. A faint line appears under the friendly name to indicate that it is recognized as a Smart Tag. Move the pointer over the friendly name, and the Smart Tag Action button appears.
 * 3) Click the Smart Tag button, and select one of the custom action items from the drop-down menu. You can send an e-mail or instant message to the contact from your new document.

You can use similar steps to test the Smart Tag DLL in Excel 2002.

back to the top

Troubleshooting
If you have problems getting your custom Smart Tags to work, first verify that the custom Smart Tag DLL is being loaded. In Word or Excel, on the Tools menu, click Auto Correct Options, click the Smart Tag tab, and verify that Label Text with Smart Tags is selected and that your Smart Tag DLL is listed and selected. If your Smart Tag is not listed, it may not be properly registered.

If the execution of the custom Recognizer or Action class is the source of the problem, you can debug a Smart Tag DLL as you would any Visual Studio .NET DLL. From Project Properties, click Configuration Properties and then click Debugging. Select Start External Program and specify the path to Winword.exe. Set a breakpoint in the New method for the class item you want to debug. When Excel or Word starts and loads the Smart Tag, your code breaks at the breakpoint and you can step through the code for debugging.

Your Smart Tag DLL should trap and handle any run-time errors. If an error is raised in your code and the error is not handled, Office immediately unloads the DLL and clears the item so that it is not loaded again. This behavior is the same as the way Office treats COM Add-ins, and can give you the impression that Office is not loading the DLL when it is in fact doing so, but was forced to unload it because of an unhandled run-time error.

back to the top

<div class="references_section">