Skip to main content
Email Password   helpLost your password?

Introduction

This document contains information on the creation of Smart Tags for Microsoft Office. This focuses on the ISmartTagRecognizer and ISmartTagAction interfaces that work with Office XP and 2003. Their newer counterparts, ISmartTagRecognizer2 and ISmartTagAction2, will not be discussed here because although Microsoft has published information about these interfaces and their new features (cascading menus, dynamic captions, backwards compatibility, etc), they have not yet published much information on their actual implementation***. As an example to go along with my explanation, I will use the creation of a simple smart tag (named SimpleTerm) that recognizes different coffee flavors for a make believe company.

***EDIT: Refer to John Liu's message at the bottom for more info on Smart Tags 2

Notes on Smart Tags

If you try to create two smart tags that recognize the same text, the application will arbitrarily pick one of the two smart tags to run. You could also disable the smart tag that you do not wish to run in the application settings. One smart tag I have made was to recognize phone numbers and then give the user options of formatting the number. This smart tag was chosen over Microsoft Office�s smart tag, which probably looks for the phone number in Outlook or adds it into your address book (I couldn�t get Microsoft�s version to recognize any phone numbers).

If you wish to put smart tags into a document and send it to someone else, you can embed them in the file. This puts information into the document that the smart tag exists along with a URL where the user may download the smart tag (if the URL was provided by the smart tag�s programmer) The user you send the file to will then be notified that the smart tags exist and will be prompted to download the whole smart tag from that URL. If you receive a document and wish to scan it to see if the smart tags on your computer recognize any text, you can do so in the smart tag options of your application.

Smart Tags do not work in IE6.

Making the Smart Tag

Create the Resource Libraries and Install Them in the Global Assembly Cache

For security reasons, every library you use must have a strong name and must be registered in the global assembly cache (even your final .dll smart tag file). Since the Smart Tag Library lacks a strong name, we must create a strong-named wrapper library for it. First we�ll create a strong name for this library and then install it into the GAC.

  1. Open a Visual Studio Command Prompt (Start -> Programs -> Visual Studio .NET -> Visual Studio .NET Tools -> Visual Studio .NET 2003 Command Prompt)
  2. Create a strong name by issuing the command:
    sn �k SmartTagLib.snk
  3. Here you could also create a strong name for your final .dll file:
    sn �k SimpleTerm.snk
  4. Next move to the directory C:\Program Files\Common Files\Microsoft Shared\Smart Tag and type the following to create a strong named wrapper for the Strong Tag Library:
    tlbimp mstag.tlb /keyfile:c:\SmartTagLib.snk /out:c:\SmartTagLib.dll

    Notice that the library was originally mstag.tlb and now a new library named SmartTagLib.dll is located at the root directory of drive C. The path to SmartTagLib.snk may differ if you created it in another folder.

  5. Issue the following command to install this DLL into the GAC
    gacutil /i c:\SmartTagLib.dll
  6. Create strong names and install other libraries that you will be using as resources

Create and Install the Smart Tag DLL

Open Visual Studio .NET and create a new Visual C# Class Library. I named my project SimpleTerm and created two classes, SmartTagRecognizer (to recognize the Smart Tag) and SmartTagAction (to perform an action on the Smart Tag). In the Solution Explorer, Right Click on References and choose to Add Reference. Click on Browse and locate all the strong named libraries that you created and will be using and add them.

Add using SmartTagLib to the top of your classes along with the names of other libraries you are using Create your Recognizer Class, implementing SmartTagLib.ISmartTagRecognizer. ISmartTagRecognizer has 6 properties (ProgID, Name, Desc, SmartTagCount, SmartTagDownloadURL, SmartTagName) and one method (Recognize()). Here is a description of the ISmartTagRecognizer interface along with examples from my SimpleTerm project. The Smart Tag SDK v1.1 also has a description of the Smart Tag interfaces and is downloadable from Microsoft�s website

public void Recognize(string text, SmartTagLib.IF_TYPE dataType, int localeID, SmartTagLib.ISmartTagRecognizerSite recognizerSite)

Note that presently onlyIF_TYPE_PARA and IF_TYPE_CELL are used (according to Smart Tag SDK 1.1, which will be outdated when the new Smart Tag SDK 2.0 is released). localeID is the per-paragraph language identifier for the string being passed in

RecognizerSite is the interface that host applications expose so that recognizers can get property bags and commit smart tags. RecognizerSite's GetNewPropertyBag() method returns a property bag that can hold key/value pairs to be passed on to the action class once the smart tag has been committed. Its other method, CommitSmartTag(), tells the ISmartRecognizerSite interface to add a new smart tag based on the passed in text. Its syntax is as follows:

void CommitSmartTag(string smartTagName, int start, int length, ISmartTagProperties Properties)

The ISmartTagProperties interface is what is used as the property bag. Its properties are : -

Count: an integer of how many properties are stored, KeyFromIndex: returns a key string corresponding to a passed index integer, Read: returns a value string corresponding to the passed key string, and ValueFromIndex: returns a value string corresponding to a passed index integer. It has one method, Write(string key, string value) to write a key/value pair to it

Now create your Action Class, implementing SmartTagLib.ISmartTagAction. ISmartTagAction has 10 properties (ProgID, Name, Desc, SmartTagCount, SmartTagName, SmartTagCaption, VerbCount, VerbID, VerbCaptionFromID, VerbNameFromID) and one method (Invokeverb()).

 public void InvokeVerb(int verbID, string applicationName, Object target, SmartTagLib.ISmartTagProperties properties, string text, string xml)

VerbID is the unique ID of the verb to invoke. applicationName is a string that represents the name (ProgID) of the calling application. This is an object-model-agnostic way of getting the name of an application. It is also used to identify applications that do not support the passing in of a Target. target is a reference to the application-specific object that is responsible for calling the smart tag. In Excel, it is an Excel range object that points to the cell that the smart tag was attached to. In Word, it is a Word range object that wraps around the block of text that the smart tag refers to. However, note that this object may be any other kind of pointer to an object appropriate for the calling application. It could also be null. properties is the smart tag property bag. An action DLL can read, write, and modify these properties so that future action invocations can take advantage of this information. text is a read-only string that represents the text of the smart tag. For example, if a smart tag on a cell reads �MSFT�, text would equal �MSFT�. xml is a read only string that is an XML representation of the smart tag

After making these classes, a strong name key needs to be assigned to the project so the library will have strong name after its compiled. Open AssemblyInfo.cs and replace the line that reads

[assembly: AssemblyKeyFile(��)]
with
[assembly: AssemblyKeyFile(�C:\\SimpleTerm.snk�)]
This uses the key you created in earlier. The name and location of the key may be whatever you specified.

Now you need to tell Visual Studio .NET to register you DLL when you compile it. To do this go to your Project Properties, click on Configuration Properties -> Build and set Register for COM Interop to True.

Build the solution. Open a Visual Studio Command Prompt and go to the bin\Debug directory of your project. Install your DLL with the following command:

gacutil /i SimpleTerm.dll
(replace �SimpleTerm� with your library�s name)

If you compile your project multiple times, the classes� CLSIDs will keep changing, which could lead to errors. To prevent this, open regedit and go to HKEY_CLASSES_ROOT. In this registry you should be able to find two subkeys by the names of your classes given in the format library.class. For my example it is SimpleTerm.SmartTagAction and SimpleTerm.SmartTagRecognizer. As subkeys for both of these, you will find each class�s CLSID. For each class, copy the CLSID and insert the following right above your class declaration:

[System.Runtime.InteropServices.GuidAttribute(<CLSID>), 
System.Runtime.InteropServices.ProgIdAttribute()]
This will tell the compiler what to make your CLSID and your ProgID. For the SmartTagRecognizer class in our example the entry would look like:
[System.Runtime.InteropServices.GuidAttribute("69D56959-B1C2"
+ "-3987-80F2-845E73197626"), 
System.Runtime.InteropServices.ProgIdAttribute( "SimpleTerm"
+ ".SmartTagRecognizer")]
Once the CLSIDs are set, you need to insert them into a place in the registry where applications look for smart tag DLLs. Locate the following key in the registry:
HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Actions
Here you will find many subkeys with CLSID values. Add the CLSID of your Smart Tag Action class here. Add the CLSID of your Smart Tag Recognizer class to the subkeys of
HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Recognizers
Note that with the newer Smart Tags for Office 2003, CLSIDs can be entered in HKEY_LOCAL_MACHINE to install the smart tag for all users of a computer

.NET also has a problem with referencing your DLL from certain applications. This is because the applications find your DLL through another DLL called mscoree.dll. This is done for security reasons in .Net. If an application is unable to load mscoree.dll when it starts, your DLL will not be referenced either. To ensure that mscoree.dll can be located by the application you must edit registry keys via regedit. Go to HKEY_CLASSES_ROOT\CLSID and locate the CLSIDs of both of your classes. Both of these keys will have a subkey called InprocServer32 and this will have a default value of mscoree.dll. Change this value to C:/Windows/System32/mscoree.dll for both of your classes. With this full pathname, your application should be able to correctly locate mscoree.dll. If you cannot find your CLSID in the registry, see if restarting regedit helps.

Note: You will have to change this value every time you compile

Your Smart Tag should now be working. If it doesn�t seem to be loading, check your security settings. For Word, you can adjust your security settings by going to Tools -> Macro -> Security. On the Trusted Publishers tab, make sure you have the Trust all installed add-ins and templates box checked. If your smart tag is loading but not functioning properly, you can debug it through Visual Studio .NET by going to Project Properties -> Configuration Properties -> Debugging. Change the Debug Mode to Program and select your Start Application to be the .exe file of the application you wish to test your smart tag with. For example, if you are trying to make a smart tag for Word, you would put C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE as your Start Application. If you are still having problems with your smart tag, refer to the FAQ of the Smart Tag SDK.

Additional References

All these contain information about the new Smart Tags API. The last link contains the most detailed information I�ve found about the actual methods, but I still found it lacking.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralPersona name smart tag is not displayed for MS Outlook 2002 (Office XP) Pin
sunil_2008
22:14 10 Aug '09  
GeneralSmart tag of persona menu not display for very high security in Outlook Pin
sunil_2008
21:50 10 Aug '09  
QuestionSmart Tag does not work with Vista and Windows Office 2003 Pin
deepwebdunia
7:36 29 Jun '09  
Generalnot register smart tag? Pin
elahe babaee
0:29 12 Aug '07  
GeneralRe: not register smart tag? Pin
The Orange Rider
21:17 13 Aug '07  
GeneralHow can get text Pin
lalune
20:01 25 Apr '07  
Generalthere is some book Pin
DiegoDotNet
16:49 8 Nov '04  
GeneralD:\Documents and Settings\Pawel\Moje dokumenty\Visual Studio Projects\SimpleTerm\SmartTagAction.cs(2): The type or namespace name 'SimpleTermLib' could not be found (are you missing a using directive or an assembly reference?) Pin
pawel.manu
3:07 5 Nov '04  
GeneralRe: D:\Documents and Settings\Pawel\Moje dokumenty\Visual Studio Projects\SimpleTerm\SmartTagAction.cs(2): The type or namespace name 'SimpleTermLib' could not be found (are you missing a using directive or an assembly reference?) Pin
ederekb1
9:04 6 Mar '06  
GeneralSmart Tags in Visual Studio Editor?? Pin
Boniolopez
2:17 15 Jun '04  
GeneralRe: Smart Tags in Visual Studio Editor?? Pin
The Orange Rider
22:16 18 Jun '04  
GeneralCOM+ Pin
Magikos
8:10 10 Jan '04  
GeneralRe: COM+ Pin
The Orange Rider
23:11 11 Jan '04  
GeneralThanks for the usefull article (+ question) Pin
RNeeman
4:02 26 Nov '03  
GeneralRe: Thanks for the usefull article (+ question) Pin
The Orange Rider
15:55 28 Nov '03  
GeneralReally good :-) Pin
Nishant S
19:13 17 Sep '03  
GeneralA good link to code for Smart Tags 2 + Office 2003 Pin
John Liu
21:45 8 Sep '03  
GeneralWhat are smart tags? Pin
Marc Clifton
2:12 4 Sep '03  
GeneralRe: What are smart tags? Pin
The Orange Rider
9:14 4 Sep '03  


Last Updated 8 Sep 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009