Using Smart Tags in Office 2003 (cont'd)
 
Figure 6: The Add Reference dialog box shows that the Smart Tags 2.0 Type Library is selected.
Building the Smart Tag Recognizer
Now that you have looked at the interfaces to implement, let's get started creating the smart tag. You will build the smart tag using Visual Basic .NET (VB.NET). First, you need to start a new VB.NET Class Library project. Then add a reference to the Microsoft Smart Tags 2.0 Type Library to the project. This type library is on the COM tab of the Add Reference dialog box, as shown in Figure 6. Note that you don't have to add both the 1.0 and 2.0 versions. Version 2.0 of the type library has all the necessary interfaces.
advertisement


Next, you'll need to add two Imports statements to the top of the source code:
   Imports SmartTagLib
   Imports System.Runtime.InteropServices
You need System.Runtime.InteropServices, as smart tags use COM Interop. And you'll need to register the DLL so that the Office applications can find it. Next, you'll create the Recognizer class. The entire code for the class is shown in Listing 3. I'll break down the less self-explanatory elements here.

The first things to look at are the ProgID, GUID, and ComVisible attributes applied to the class. These are for use by COM Interop services. You can create a GUID using the Create GUIDoption on the Tools menu. Use option 4. Registry Format, to generate the correct style for use here. Notice that the curly brackets have been removed, a necessary step to prevent compile errors.

The class is named Recognizer and implements both Recognizer interfaces. I have added a Public Sub New constructor to the class, necessary for COM Interop. Following the constructor, there are several properties that are pretty self-explanatory. Notice that the Recognize method is not implemented. Instead, you'll use the Recognize2 method, and you'll take a look at this method a little later. Next, there are a few more self-explanatory properties. The one that takes a little explanation is the SmartTagName property.

The SmartTagName property is the unique identifier for the smart tag type(s) that the recognizer supports. Each smart tag type is defined by a namespace to keep it unique. A namespace is an XML construct uniquely identifying a group of XML tags belonging to a logical category. A namespace groups related properties together for easy property discovery and, more importantly, to keep the property names unique.

Office 2003 adds smart tag support to PowerPoint 2003 and Access 2003.
Property names are constructed from a namespace URI and a tag name of the namespace URI.

Similarly, smart tag types are defined by a unique namespace URI plus its tag name. A pound character (#) is added to the namespace URI and is used to separate the namespace URI from its tag name.

For this example, you'll be using a smart tag of the type urn:samples-msdn-microsoft-com#MySmartTag. The namespace URI is urn:schemas-microsoft-com, and MySmartTag is the tag name. The type and name are combined to form a fully qualified name for the property, which, in this case, is a smart tag type.

As can been seen, the name is a fully qualified description of an XML tag. Every smart tag type is uniquely identified by its tag name in addition to its namespace.

The URI portion of the property name ensures that it is globally unique and unambiguous. Two properties with the same tag name can be differentiated using namespaces.

The role of an ISmartTagRecognizer is to assign smart tag types to data. For example, the recognizer sees the string SmartTag and assigns to it the smart tag type urn:samples-msdn-microsoft-com#MySmartTag, as you'll see in the Recognize2 method.

Now look at the Recognize2 method. This is where the real recognizer is implemented. Look for the term SmartTag in the text. If you find it, you collect some information and store it in a RecognizerSite PropertyBag object. Then you call the CommitSmartTag method of the RecognizerSite object passing the smart tag type as the first parameter. This is what "tags" the text as recognized.

Previous Page: MOSTL and Regular Expressions Next Page: Building the Smart Tag Action
Page 1: IntroductionPage 3: Building the Smart Tag Recognizer
Page 2: MOSTL and Regular ExpressionsPage 4: Building the Smart Tag Action