devxlogo

Using Smart Tags in Office 2003

Using Smart Tags in Office 2003

f you developed smart tags in Office XP, you’ll be interested in the changes in Office 2003. Smart tag technology links text to resources that provide relevant information useful in creating a document. Or even better, relevant information can be inserted right into the document itself. Smart tags in Microsoft Office 2003 have been improved to make them even easier to develop, and they conquer a few limitations.

Smart tags were introduced in Microsoft Office XP, providing a new way to make more meaningful data in documents. A major change in Office 2003, the number of applications supporting smart tags has increased. In Office XP, smart tags were supported in Word, Excel, Outlook (with Word as the e-mail editor), and Internet Explorer, to a limited extent. In Office 2003, all these applications are supported and both PowerPoint 2003 and Access 2003 have been added.

New features have been added to the Microsoft Office Smart Tag Lists (MOSTL). MOSTL provides the capability to define smart tag recognizers in XML. Although it’s not as flexible as implementing smart tags in a programming language, it’s great when you have an easily defined list of words or phrases to be recognized. You can use regular expressions to define search patterns for your recognizer and you’ll see a few examples using MOSTL and regular expressions later in this article.

In addition, the API library has been extended, supporting several new interfaces and enabling new functionality. The Microsoft Smart Tags 2.0 Type Library is backward compatible with the 1.0 version, allowing you to develop smart tags that work in both Office XP and Office 2003. This type library and an example of implementing a smart tag in .NET are discussed in more detail later in this article.

An Introduction to Smart Tags
Smart Tags identify words or phrases within a document, and several are provided with Microsoft Office. For a list of existing smart tags, and to enable and disable smart tags, go to Tools (from within any of the applicable Office 2003 applications) and then choose Auto Correct Options. Click on the Smart Tags tab and you’ll see a list of available smart tags.

Figure 1: The smart tag shows the financial symbol and its smart tag drop-down menu.

To experiment, enable the Financial Symbols smart tag. Then enter a stock symbol, such as MSFT, in a Word 2003 document. When the symbol is recognized, smart tags make a red-dotted underline under it. When you hover your mouse pointer over it, you’ll see an information icon. Clicking on the icon displays a drop-down menu, as shown in Figure 1. The menu shows a list of actions relevant to the financial symbol.

Another smart tag might look up contact information in your Outlook 2003 contact list. Figure 2 shows the drop-down menu and the actions available with that smart tag.

Although these are good examples of smart tags in action, the number of provided smart tags is limited. It is possible to create your own smart tags using MOSTL or by developing them in a programming language. Then the possibilities of what you can do with smart tags are virtually endless!

MOSTL and Regular Expressions
Regular expressions are a powerful form of wildcard searches. Most developers (and even power users) are familiar with regular expressions used to search for files, such as *.doc. When it comes to regular expression pattern matching, you will need more sophisticated patterns. The following pattern matches US phone numbers within a text string.
   (((d{3}))|(d{3}-))?d{3}-d{4}

If you are not familiar with regular expressions, you might find this but of code a little difficult to read. Let’s take a brief look at the syntax of regular expressions. Table 1 lists the most common meta-characters that are used in regular expression patterns.

Table 1: This Smart Tag Meta-character listing will make iteasy to follow the exploration.

Meta-Character Meaning
Character Matching ?
. (period) Matches any single character except the newlinecharacter
[] Matches any one of the enclosed characters. Rangescan be specified by using a hyphen, such as [0-9]
| The OR operator. For example, x|y will matcheither x or y
Position Matching ?
^ Matches the beginning of string
$ Matches the end of string
Repetition Matching ?
? Matches 0 or 1 instances of the precedingcharacter
+ Matches 1 or more instances of the precedingcharacter
Indicates that the next character should not beinterpreted as a regular expression specialcharacter
* Matches 0 or more instances of the precedingcharacter
() Groups a series of characters together
{n} Matches exactly n instances of the precedingcharacter
{n,} Matches at least n instances of the precedingcharacter
{n,m} Matches at lease n but no more than m instances ofthe preceding character
Special Characters ?
s Matches any single white space character,including space, tab, line feed, and form feed
w Matches any alphanumeric character, including theunderscore character
d Matches a single-digit character
f Matches a form feed
Matches a line feed
Matches a carriage return
Matches a tab
v Matches a vertical tab
Matches a word boundary, such as a space

Examine this by breaking down the phone number pattern shown above. Start at the end of the pattern:

   d{3}-d{4}

This means that the expression must contain exactly three digits, followed by a hyphen, and then exactly four digits. Thus, 555-1234 is a match. Now look at the first part:

   (((d{3}))|(d{3}-))?

Notice that there is a pipe (|) in the middle, indicating an or operator. On the right side of that you can see:

   (d{3}-)

This will match exactly three digits followed by a hyphen. On the left side of the or (|) you see:

   ((d{3}))

Notice that ( and ) are used to indicate that parenthesis are part of the pattern. In between these is the d{3} indicating exactly three digits. So the pattern matches phone numbers in these formats:

   (281)866-7444   281-866-7444 

This portion of the pattern ends with a question mark, indicating that the pattern immediately prior to the question mark is optional. Because the area code pattern is grouped in parentheses and the question mark is outside the parentheses, the entire pattern inside the parentheses is optional. The pattern will also be a match:

   866-7444

You could get even more sophisticated and match 281.866.7444 in addition to allowing a space between the various parts of the phone number and match foreign phone numbers.

Here’s another example. Let’s say you use a bug-tracking Web site to track issues that need to be addressed in your current project. You can define a smart tag that recognizes “Bug #123456” or “bug 123456” and link directly to that bug’s details on the Web site. Here is the regular expression that matches these phrases:

   [B|b]ugs(#){0,1}d{6}

You can create a smart tag definition to recognize this pattern and link to the Web site. It looks like Listing 2. Note that the URL in this example is a placeholder that should be replaced with the URL to your bug-tracking Web site. The resulting smart tag menu is shown in Figure 4.

Building the Smart Tag Action
Now that you recognize the terms, you need to build the actions. The code for the Action class is shown in Listing 4.

Again, use the attributes necessary for COM Interop. Make sure to generate a different GUID for the Action class. The properties for the Action class are similar to those found in the Recognizer class.

The Action class uses verbs corresponding with the menu options you define to be displayed in the on-application menu. The VerbCount property defines how many verbs your Action class supports.

Because you are only supporting one smart tag in this example, the VerbID property just returns the VerbIndex. The ShowSmartTagIndicator property defines whether the recognized terms are underlined, like regular smart tags. In most cases you’ll want this, so return True for this property.

The VerbCaptionFromID property is called one time for each verb you are supporting: six, in this case. It takes a VerbID as a parameter, which is used in the CASE statement to build the cascading menu.

Now take a look at the InvokeVerb2 method. In the example, you collect information gathered in the Recognizer class along with the VerbID and Application Name, and you display a message box with this information.

Now you can compile the DLL. Before you test it, you must first register the two classes with the Office 2003 smart tag infrastructure so that the Office 2003 applications will know to use the smart tag.

Registering Smart Tag Classes
There are two ways to register your smart tag classes with the smart tag infrastructure. One way is to manually add keys to the HKEY_CURRENT_USERSoftwareMicrosoftOfficeCommonSmart Tag registry key. There is a key for Recognizers and a key for Actions. You need to add a new key to each section for the appropriate class using the GUIDs specified in the class attributes in your code. Make sure to add the curly brackets back to the GUIDs in these keys.

The second way to do this is to add code that uses reflection to your classes in order to register the smart tag at the same time the smart tag is registered with COM. This is the preferred method, especially if you will be distributing your smart tags. But it takes a bit of code to do it. To see how this is done, refer to the Technical Article on MSDN, “Building Smart Tags in Microsoft Visual Basic .NET,” by J Sawyer.

Smart tags have been improved quite a bit in Office 2003. And more Office 2003 applications support smart tags than in Office XP. Developers get new flexibility in creating smart tags using MOSTL combined with regular expressions to define Recognizers. You also get a lot of power developing smart tags in VB.NET or C# with the new version of the Smart Tags 2.0 Type Library.

devx-admin

Share the Post: