Professional Documents
Culture Documents
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.
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 Offices smart tag, which probably looks for the phone number in Outlook or adds it
into your address book (I couldnt get Microsofts 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 tags
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 well 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:
Collapse
Copy Code
sn k SmartTagLib.snk
3. Here you could also create a strong name for your final .dll file:
Collapse
Copy Code
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:
Collapse
Copy Code
Copy Code
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.
Copy Code
Name is a short title that reflects what the recognizer does. This is what appears in the
Smart Tag Menu
Collapse
Copy Code
Copy Code
SmartTagCount specifies the number of smart tag types that the recognizer supports
Collapse
Copy Code
Copy Code
Person A creates an e-mail message with several CameraName smart tags in it and sends
it to person B. Ordinarily, because person B does not have the CameraName DLL
installed on their machine, the annotated data cannot be seen. Smart-tagging the e-mail
message would have been a pretty useless exercise on the part of person A. But because
the CameraName recognizer embeds a download URL, at the very least, person B can
take advantage of the fact that there are CameraName smart tags in the documents by
going to the Web page to download the actions and install the CameraName
recognizer/action smart tag DLL.
SmartTagNames are the unique identifiers of smart tag types that the recognizer
supports. The strings that are returned by this method must conform to the
"namespaceURI#tagname" key style that defines a smart tag
Collapse
Copy Code
Recognize() This is the main method where all the action happens. Text is recognized as a
smart tag type and committed for the Action class to handle.
text is the string that is searched for smart tags. dataType an enumerator that describes the
type of text being passed to the recognizer
IF_TYPE_REGEXP applies to text being passed to the recognizer after filtering using
pattern matching.
IF_TYPE_CELL applies to text being passed to the recognizer a cell at a time (as in
Excel, for example).
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)
o smartTagName is a smart tag type string, in the form of
namespaceURI#localname
o start is an integer (1- based) which specifies the start position of the recognized
smart tag in a recognized text
o length is a count of the number of characters that the smart tag will span
o properties is the ISmartTagProperties object previously obtained from
GetNewPropertyBag()
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()).
ProgID is the programmatic identifier of the action interface. Its implementation is just
like ISmartTagRecognizersProgID
Collapse
Copy Code
SmartTagCount specifies the number of smart tag types that the recognizer supports
(similar to ISmartTagRecognizers)
SmartTagNames are the unique identifiers of smart tag types that the dll supports. The
strings that are returned by this method must conform to the "namespaceURI#tagname"
key style that defines a smart tag (similar to ISmartTagRecognizers)
SmartTagCaption is a caption for a smart tag type for use in the menu for the Smart Tag
Collapse
Copy Code
VerbCount is the total number of verbs, or actions, that are supported by this DLL for a
given smart tag type
Collapse
Copy Code
if (smartTagName == "schemas-fourth-" +
"com/fourthcoffee#flavor")
return 1;
else
return 0;
}
smartTagName is a smart tag type string in the form of namespaceURI#tagname
Copy Code
VerbCaptionFromID is a caption for a verb that appears on the Smart Tag Menu
Collapse
Copy Code
Copy Code
InvokeVerb() invokes the appropriate verb when a user clicks on an action displayed in
the smart tag UI that corresponds to the VerbCaptionFromID value.
Copy Code
[assembly: AssemblyKeyFile()]
with
Collapse
Copy Code
[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:
Collapse
Copy Code
gacutil /i SimpleTerm.dll
(replace SimpleTerm with your librarys 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 classs
CLSID. For each class, copy the CLSID and insert the following right above your class
declaration:
Collapse
Copy Code
[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:
Collapse
Copy Code
[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:
Collapse
Copy Code
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
Collapse
Copy Code
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 doesnt 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.