Chapter 14. Developing Office XML applications Office Script Developer Task

  • Smart documents

  • Smart tags

  • The research pane

  • XML expansion packs and manifests

Previous versions of Microsoft Office allowed developers to create custom applications that work within the Office environment. XML in Office 2003 improves Office custom applications, for two major reasons:

  • Office can process more than its traditional binary .doc and .xls files. These days, almost any data is available as XML, which means it can be edited by Office. Complex applications can be built to perform completely customized tasks, but still provide the familiar features of the Office environment.

  • XML allows the elements of a document to be identified unambiguously. Applications can therefore be accurately context-sensitive when interacting with the user.

A complete explanation of how to develop Office-based applications is outside the scope of this book. This chapter covers some of the key XML-enabled aspects, including:[1]

  • Smart documents, which allow specialized, context-sensitive task panes to appear, depending on the current element type. Users can interact with controls in the task pane.

  • Smart tags, which allow context-sensitive menus to appear when a sequence of characters is recognized that matches a list or pattern.

  • The research pane, which allows customized research services to be used from an Office task pane.

  • XML expansion packs and manifests, which facilitate the deployment of Office applications.

Smart documents

Smart documents are documents for which there are customized, context-sensitive, Document Actions task panes that assist the user in working with the document. They are context-sensitive in that the task pane changes depending on the type of element in which the cursor is positioned.

Some applications of smart documents include:

  • Providing customized help for a specific set of users. Medical personnel filling out a health insurance claim form could be provided with a help pane whose content changes as they navigate from field to field.

  • Allowing users to access data from a variety of sources by clicking on the task pane. Users preparing legal documents could include different types of boilerplate text from other documents or from a Web service.

  • Guiding users through a process. Employees filling out an expense report can be guided step by step (calculate totals, validate, submit, etc.) based on the information that has already been completed in the worksheet.

  • Providing context-sensitive data. Customer service representatives responding to a customer complaint can choose an explanation from a drop-down list in the task pane. The list members vary depending on the nature of the complaint.

  • Allowing common actions to be performed at the click of a button. Smart document solutions can be configured to email the original author of a document when you have made revisions to it, or send a document to a Web service once it is complete.

This section explains smart documents, and provides an overview of how to build and use them in Word. We will revisit our customer letter example to illustrate the capabilities and walk you through the underlying code.[2]

A smart document solution is designed to work with XML documents that conform to a specific schema. Our letter solution, for example, will only work with documents that conform to the letter.xsd schema. You cannot use it with a non-XML Word document, or with an XML document that conforms to another schema.

Although the example uses Word, you can also use smart documents with Excel, in areas of worksheets that are mapped to the appropriate schema.

The customer letter example, revisited

In Chapter 6, “Using external XML data in documents”, on page 114 we learned about Worldwide Widget’s award-winning customer service program. The company prides itself on how quickly it responds to a customer complaint with a sincere personalized form letter. It decides to build a smart document solution to improve response time and the sincerity of the generated content.

We can add a variety of functions to the task pane, which will allow the user to:

  • see different help text depending on where the cursor is positioned

  • select a value for salutation or format from a group of radio buttons, as shown in Figure 14-1

    A smart document pane with radio buttons

    Figure 14-1. A smart document pane with radio buttons

  • select boilerplate text to insert into the body of the document, as shown in Figure 14-2

    Listing document fragments for insertion

    Figure 14-2. Listing document fragments for insertion

  • press a button to fill in the customer address based on the customer name

  • link to websites for further assistance

A working customer letter example named LetterSample.doc can be found in the example files on the book’s website.

Installing a smart document solution

Word documents and templates, and Excel workbooks, have to be associated with smart document solutions much like they need to be associated with schemas. Every smart document solution has a manifest, which is an XML document that lists all the components of the solution and their locations. We will look more closely at manifests in 14.4, “XML expansion packs and manifests”, on page 346.

To associate a smart document solution with a Word document:

  1. Open the document, in this case letter_smart.doc, in Word. It does not matter whether the letter schema has been added to the schema library yet.

  2. Click Templates and Add-ins on the Tools menu.

  3. Click the XML Expansion Packs tab, which results in the dialog shown in Figure 14-3.

    The XML Expansion Packs tab of the Templates and Add-ins dialog

    Figure 14-3. The XML Expansion Packs tab of the Templates and Add-ins dialog

  4. Click Add. In the resulting dialog, browse to the location of letter_manifest.xml and select it.

  5. This will return you to the Templates and Add-ins dialog, this time with the attached solution listed as an available expansion pack, as shown in Figure 14-4.

    The XML Expansion Packs tab with expansion pack added

    Figure 14-4. The XML Expansion Packs tab with expansion pack added

  6. Click OK to return to your document.

Attaching the manifest not only associated the smart document solution with your document, it associated a schema (which is listed in the manifest) with your document. You can now use the example to test out the capabilities of smart documents.

How smart documents work

The smart document solution is sensitive to element types. Whenever the mouse is clicked within an element, the Document Actions task pane displays user controls that are associated with that element type.

In Figure 14-1, for example, the cursor is currently positioned in the salutation element, which is associated with the radio button group at the bottom of the task pane. The four controls above that – help text, a hyperlink, etc. – belong to the ancestors of the current element, since the click occurred within them as well.

In this case there are two ancestors – greeting and letter – but all four of the other controls belong to letter.

Each of the two element types has a caption that introduces its controls. The caption for letter is “Writing a customer letter”, while the caption for salutation is “Salutation”.

The controls themselves may have captions and internal text. The caption for the radio group is “Select a salutation”. Its internal text appears next to each of the buttons.

If the user interacts with a control, a behavior will occur. In our example, if a radio button is clicked, its text will be inserted into the document as the content of the salutation.

In order for the smart document processing to accomplish these things, your solution must provide it with the necessary information. Specifically:

  1. The element-type names and captions for each element type that has controls.

  2. The number of controls and types of control that are associated with each element type. The complete list of control types is found in Table 14-1.

    Table 14-1. Control types

    Control-type name constant

    Description

    C_TYPE_ACTIVEX

    ActiveX control

    C_TYPE_BUTTON

    Command button

    C_TYPE_CHECKBOX

    Check box

    C_TYPE_COMBO

    Combo box

    C_TYPE_DOCUMENTFRAGMENT

    Document fragment that is specified as an XML string

    C_TYPE_DOCUMENTFRAGMENTURL

    Document fragment that is specified as a URL that returns an XML string

    C_TYPE_HELP

    Help text that is specified as an XHTML string

    C_TYPE_HELPURL

    Help text that is specified as a URL that returns an XHTML string

    C_TYPE_IMAGE

    Image

    C_TYPE_LABEL

    Label that is specified as a plain text string

    C_TYPE_LINK

    Hyperlink

    C_TYPE_LISTBOX

    List box

    C_TYPE_RADIOGROUP

    Radio button group

    C_TYPE_SEPARATOR

    Horizontal line

    C_TYPE_TEXTBOX

    Text box

  3. The desired appearance of the controls, including their captions, display properties (such as fonts), and internal text such as the strings that appear with each radio button.

  4. The behavior that occurs when a user interacts with the controls, for example by pressing a button or selecting an item from a list.

Tip

Tip

If you are able to limit your solution to using only hyperlink, separator, label, image, embedded help, and button controls (with buttons limited to hyperlink actions), you can provide this information in XML without writing any program code. The Smart Documents SDK has the details on this. It is an extension of the smart tags list that we discuss in 14.2.1, “Using smart tag lists (MOSTL)”, on page 338.

The smart document interface

The code for the letter solution is written in Visual Basic 6.0.[3] The Visual Basic project can be found in the example file LetterSample.vbp. It consists of a single class file, clsActions.cls, that is used to make LetterSample.dll.

The class implements the properties and methods of the ISmartDocument interface.[4] The interface has eight properties of element types and controls, and 17 methods for initializing controls and responding to interactions, as follows:

element-type properties

When a smart document is opened, or a smart document solution is attached to an open document, Office gets the values for element-type properties from your code. These include the number of element types that have controls, the element-type names and captions, and the number of controls for each element type.

control properties

Office also gets properties of each individual control, including its type, caption, and a numeric ID (ControlID).

control rendering methods

When the user clicks in an element, Office invokes a method for each of the element type’s controls. The method supplies the information needed to render the control in the task pane. The method names are usually derived from the control type names; for example, PopulateRadioGroup, our implementation of which is shown in Example 14-8.[5]

control action methods

When the user interacts with a control, Office invokes a method that performs the desired behavior. The method name is usually derived from the control type name; for example, OnRadioGroupSelectChange, our implementation of which is shown in Example 14-10.

The number of properties and methods that your class needs to implement depends on the types of control you use. In the following sections we will see how our example class implements these properties and methods.[6]

Element-type properties

The beginning of the class code is shown in Example 14-1. The third line defines the constant cTYPES to be the number of element types with associated controls. Office gets this value from the SmartDocXmlTypeCount property. It is the basis for initializing the other element-type properties.

Example 14-1. Defining constants for later reference

Option Explicit
Implements ISmartDocument
Const cTYPES As Integer = 10

Const cNAMESPACE As String = "http://xmlinoffice.com/letter"

Const cLETTER As String = cNAMESPACE & "#letter"
Const cDATE2 As String = cNAMESPACE & "#date"
Const cADDRESS As String = cNAMESPACE & "#address"
Const cSALUTATION As String = cNAMESPACE & "#salutation"
Const cCUSTOMER As String = cNAMESPACE & "#customer"
Const cBODY As String = cNAMESPACE & "#body"
Const cP As String = cNAMESPACE & "#p"
Const cFORMAT As String = cNAMESPACE & "#format"
Const cPROBLEM As String = cNAMESPACE & "#problem"
Const cEMP As String = cNAMESPACE & "#emp"

Element-type names

The smart document interface requires element-type names to be name-space-qualified. In XML that would mean declaring a namespace abbreviation and prefixing it with a colon to the element-type name. Smart document code doesn’t have XML’s syntactic constraints and doesn’t need the abbreviation; the prefix is the complete namespace URI and a pound sign.[7]

In the code, the namespace URI is assigned to the constant cNAMESPACE, which is used to construct the qualified names for the ten element types that are to have controls associated with them (e.g. cLETTER, cSALUTATION).

At this point, Office knows the number of element types that have controls, but not the element-type names. The property in Example 14-2 provides the names.

Example 14-2. Get the relevant element-type names

Private Property Get
ISmartDocument_SmartDocXMLTypeName
  (ByVal SmartDocID As Long) As String

  Select Case SmartDocID
    Case 1
      ISmartDocument_SmartDocXMLTypeName = cEMP
    Case 2
      ISmartDocument_SmartDocXMLTypeName = cADDRESS
    Case 3
      ISmartDocument_SmartDocXMLTypeName = cLETTER
    'REST OF THE CASES GO HERE
    Case Else
  End Select
End Property

Tip

Tip

When you implement this property, you are in effect assigning sequential numeric IDs to the element types. Be sure to create a case for each element type that has associated controls. Any order is acceptable.

Element-type captions

Each element type has a caption, which is displayed before all the controls for that element type. In Figure 14-1, the caption for the salutation element type is Salutation.

Office gets the captions from the property in Example 14-3, which functions the same way as the previous example.

Example 14-3. Get the element-type captions

Private Property Get ISmartDocument_SmartDocXMLTypeCaption
  (ByVal SmartDocID As Long, ByVal LocaleID As Long) As String

  Select Case SmartDocID
    Case 1
      ISmartDocument_SmartDocXMLTypeCaption = "Employee Name"
    Case 2
      ISmartDocument_SmartDocXMLTypeCaption = "Look up address"
    Case 3
      ISmartDocument_SmartDocXMLTypeCaption = _
      "Writing a customer letter"
    'REST OF THE CASES GO HERE
    Case Else
  End Select
End Property

Tip

Tip

Make sure the case numbers for the captions are the same as the case numbers for the corresponding element-type names in the previous example.

The LocaleID parameter indicates the location in which the code is running. You can use conditional logic with it to localize the captions.

Control count

In order to determine the control IDs for an element-type, Office needs to know how many controls there are. It uses the property in Example 14-4 to get the control count. For example, as we saw in 14.1.3, “How smart documents work”, on page 326, letter has four controls.

Example 14-4. Get the control count for an element type

Private Property Get ISmartDocument_ControlCount
  (ByVal SmartDocName As String) As Long

  Select Case SmartDocName
    Case cEMP
      ISmartDocument_ControlCount = 1
    Case cADDRESS
      ISmartDocument_ControlCount = 1
    Case cLETTER
      ISmartDocument_ControlCount = 4
    'REST OF THE CASES GO HERE
    Case Else
  End Select
End Property

Tip

Tip

When you implement this property, be sure to supply the correct quantity for each element type. Note that the argument is the element-type name (SmartDocName), not the numeric ID as in the previous examples.

Control properties

Once Office knows the element types and the number of controls each has, it gets the property values for each control.

Control ID

At this point, Office does not have a unique identifier for each control. For any given element type, however, it knows the number of controls. It therefore knows a sequential index number that uniquely identifies each control among the others for its element type. For example, the four controls associated with the letter element type have the index numbers 1, 2, 3 and 4.

Office gets a solution-wide unique number from the ControlID property implemented in Example 14-5, by passing it the element type name and the control’s sequential index number. For example, the one control associated with the address element type is assigned the control ID 101. The four controls associated with the letter element type are assigned 201, 202, 203 and 204.

Example 14-5. Get the unique numeric control ID

Private Property Get ISmartDocument_ControlID
 (ByVal SmartDocName As String, ByVal ControlIndex As Long) As Long

  Select Case SmartDocName
    Case cEMP
      ISmartDocument_ControlID = ControlIndex
    Case cADDRESS
      ISmartDocument_ControlID = ControlIndex + 100
    Case cLETTER
      ISmartDocument_ControlID = ControlIndex + 200
    'REST OF THE CASES GO HERE
    Case Else
  End Select
End Property

Control type

Office now has a numeric ID for each control, but not the control type. It gets that information from the property shown in Example 14-6.

Example 14-6. Get the control type

Private Property Get ISmartDocument_ControlTypeFromID
 (ByVal ControlID As Long, ByVal ApplicationName As String,
  ByVal LocaleID As Long) As SmartTagLib.C_TYPE

 Select Case ControlID
   Case 1
     ISmartDocument_ControlTypeFromID = C_TYPE_TEXTBOX
   Case 101
     ISmartDocument_ControlTypeFromID = C_TYPE_BUTTON
   Case 201
     ISmartDocument_ControlTypeFromID = C_TYPE_HELP
   Case 202
     ISmartDocument_ControlTypeFromID = C_TYPE_LABEL
   'REST OF THE CASES GO HERE
   Case Else
 End Select
End Property

In the example, the control whose ID is 202, which is the second control associated with the letter element type, is a label. A complete list of control types is shown in Table 14-1.

The ApplicationName parameter indicates the application in which the code is running. You can use conditional logic with it to specify different control types for Word and Excel.

Control caption

We have seen that each element type has a caption. Some control types also allow captions. In Figure 14-1, the caption for the salutation radio group is Select a salutation.

Office gets the control captions from the property implemented in Example 14-7. The control whose numeric ID is “1” has the caption “Please enter your name:”.

Example 14-7. Get the control caption

Private Property Get ISmartDocument_ControlCaptionFromID
  (ByVal ControlID As Long, ByVal ApplicationName As String,
  ByVal LocaleID As Long, ByVal Text As String,
  ByVal Xml As String, ByVal Target As Object) As String

  Select Case ControlID
    Case 1
      ISmartDocument_ControlCaptionFromID = _
          "Please enter your name:"
    Case 101
      ISmartDocument_ControlCaptionFromID = _
          "Look up address"
    Case 201
      ISmartDocument_ControlCaptionFromID = _
          "Overview"
    Case 202
      ISmartDocument_ControlCaptionFromID = _
          "Please see below for context-specific help, " + _
          "or follow this link to the Intranet"
    'REST OF THE CASES GO HERE
    Case Else
  End Select
End Property

Additional parameters that we have not yet seen allow more criteria for conditional logic to modify the captions.

  • Text is the data content of the current element, concatenated with the data content of any subelements.

  • Xml is the XML representation of the current element (which, by definition, includes its attributes and subelements).

  • Target is the Word or Excel range object with the current element’s content. By using Target.Document you can address the entire Word document; Target.Worksheet lets you address the entire Excel worksheet.

Control initialization methods

As Office renders controls in the task pane, it invokes methods – usually specific to the control type – that specify their appearance and internal content. The method names begin with Populate and are typically derived from the control type name; for example, the method PopulateRadioGroup is called to initialize a radio group. An implementation of this method is shown in Example 14-8.

Example 14-8. Initializing a control

Private Sub ISmartDocument_PopulateRadioGroup
  (ByVal ControlID As Long, ByVal ApplicationName As String,
   ByVal LocaleID As Long, ByVal Text As String,
   ByVal Xml As String, ByVal Target As Object,
   ByVal Props As SmartTagLib.ISmartDocProperties,
   List() As String, Count As Long, InitialSelected As Long)

    Select Case ControlID
        Case 401
            ReDim List(1 To 3) As String
            Count = 3
            List(1) = "letter"
            List(2) = "e-mail"
            List(3) = "phone call"
            InitialSelected = -1
        Case 501
            ReDim List(1 To 4) As String
            Count = 4
            List(1) = "Mr."
            List(2) = "Ms."
            List(3) = "Mrs."
            List(4) = "Dr."
            InitialSelected = -1
    End Select
End Sub

The same method is invoked for all controls of a type, regardless of where they occur in the smart document, so it is necessary to test for the control ID if you want to perform different actions for specific controls. In our example, control 401 is a radio group for the format element type, while control 501 belongs to the salutation element type.

The Props parameter points to a structure that can be used to set display properties for the control, such as:

  • the size of the control (height and width);

  • positioning of the control (alignment, indentation and spacing between controls); and

  • fonts of text labels (face, size, style and weight).

For example, to set the properties for a label, you might use the code shown in Example 14-9.

Example 14-9. Setting the properties of a control

With Props
        .Write Key:="FontFace", Value:="Arial"
        .Write Key:="FontWeight", Value:="bold"
        .Write Key:="Align", Value:="left"
End With

In addition to parameters like Props and the others we have seen, which are passed to all the control initialization methods, there are parameters that are passed only to specific control types. For example, the List(), Count and InitialSelected parameters are passed only to methods that initialize controls that have lists.

The initialization methods are executed each time the user moves between element types. Your implementations can therefore use conditional logic and the appropriate parameters to display the controls differently depending on the state of the document. For example, if the user has already inserted a type attribute of body with a value of problem, you can display document fragments that differ from those displayed for a body type of sales.

Control action methods

When the user interacts with a control in the Document Actions task pane, Office performs the control’s built-in behavior if it has one. Clicking a hyperlink, for example, will always result in a browser being pointed toward the specified URL.

For other control types, Office calls the appropriate action method. For example, if a user clicks on a radio button, Office invokes the OnRadioGroupSelectChange method. An example implementation of this method is shown in Example 14-10.

Example 14-10. Setting the properties of a control

Private Sub ISmartDocument_OnRadioGroupSelectChange
  (ByVal ControlID As Long, ByVal Target As Object,
   ByVal Selected As Long, ByVal Value As String)

  Dim objRange As Word.Range
  Set objRange = Target.XMLNodes(1).Range
  objRange.Text = Value
  Set objRange = Nothing
End Sub

As with the initialization methods, there is only one action method for all controls of the same type. In our example, we want the same behavior regardless of which radio group control is clicked, so we do not test for the control ID. We simply copy the selected Value into the current XML element.

However, we are not limited to such a simple action. In this method, we could change any part of the current element or the document in any way, access external code or a database or Web service, perform Word tasks such as applying styles or printing, or any other action that can be expressed in code.

Smart tags

Smart tags allow actions to be associated with words and phrases in a document. The “tags” don’t have to be delimited, as XML tags are. Instead, they are defined by a program or by a lookup table that contains character strings and their associated actions. The product recognizes the matching strings in the document.

Instead of a specific character string, recognition could be based on a kind of pattern for strings that computer scientists call a regular expression. In this case, any character string in the document that matched the pattern would be recognized and considered a smart tag.

Alternatively, program code could be used to create more sophisticated recognition tests.

In all cases, though, when a user inserts or mouses over recognized text a small button appears. When clicked, a context menu pops up that contains actions relating to the recognized text.

Figure 14-5 is an example of the stock symbol smart tag that is built into Excel. The string “MSFT” in cell A1 has been recognized as a stock ticker symbol, so the smart tag i button is displayed next to the cell. Clicking the button caused the context menu to appear. The menu allows you to insert the stock price in the worksheet or to go to the MSN website for more information about the stock or the company.

A smart tag menu

Figure 14-5. A smart tag menu

Another built-in smart tag for Word recognizes full names of your Microsoft Outlook contacts. The context menu lets you send that person an e-mail or schedule a meeting with her.

Using smart tag lists (MOSTL)

The simplest way to implement smart tags is by creating an XML document that complies with the Microsoft Office Smart Tag Lists (MOSTL) schema. This document lists the text strings or patterns to look for, and the actions to show in the context menu when a smart tag is recognized.

An example of a MOSTL document for recognizing ZIP codes is shown in Example 14-11. While some MOSTL documents list literal text strings to recognize, this document specifies a pattern. The regular expression (d{5}), which represents five digits, is the content of the exp element. This means that any five consecutive digits will be recognized as a ZIP Codes smart tag.

Example 14-11. A MOSTL document to implement smart tags

<FL:smarttaglist
    xmlns:FL="urn:schemas-microsoft-com:smarttags:list">
  <FL:name>ZIP Codes</FL:name>
  <FL:lcid>1033</FL:lcid>
  <FL:description>Recognizes ZIP codes</FL:description>
  <FL:moreinfourl>http://xmlinoffice.com</FL:moreinfourl>

  <FL:smarttag
type="urn:schemas-microsoft-com:smarttags#msproducts">
    <FL:caption>ZIP Code</FL:caption>
    <FL:re>
       <FL:exp>(d{5})</FL:exp>
    </FL:re>
    <FL:actions>
       <FL:action id="ship">
          <FL:caption>Look up shipping rates</FL:caption>
          <FL:url>http://xmlinoffice.com/ship/{TEXT}</FL:url>
       </FL:action>
       <FL:action id="tz">
          <FL:caption>Determine time zone</FL:caption>
          <FL:url>http://xmlinoffice.com/tz/{TEXT}</FL:url>
       </FL:action>
    </FL:actions>
  </FL:smarttag>
</FL:smarttaglist>

Each menu item is specified with an action element that contains a caption (the text that will appear on the menu) and a url that is referenced when you click the menu item. Our example might allow users to look up shipping rates to that ZIP code, or to determine the time zone of the ZIP code. The expression {TEXT} is used within the URL to mean that the recognized string (in this case the 5-digit number) will be included in the URL at that point.

Implementing this smart tag list is a simple matter of placing the XML document in your local smart tag lists directory, and restarting all smart-tag-compliant applications. The directory path is normally: Program FilesCommon FilesMicrosoft SharedSmart TagLists

Using program code

Smart tags can also be implemented using program code, written with either Visual Studio.NET or Visual Studio 6.0.

While MOSTL documents can only specify actions as URLs, program code allows you to perform almost any action you can imagine. You can access Web services and databases, send emails, and insert or edit text or XML elements, among other things.

More sophisticated recognizers can also be written using program code, for example to only recognize valid ZIP codes, or only ZIP codes that appear in certain areas of a document.

Unlike smart documents, smart tags have no direct relationship with the Office features that allow the editing and viewing of XML documents. Smart tags can therefore be used in any Word and Excel documents, regardless of whether the documents are associated with a schema. Smart tags can also be used in PowerPoint, Access, Outlook, and Internet Explorer.

However, in Word and Excel documents that are associated with a schema, it can be useful to create smart tag solutions that are sensitive to the XML context. For example, you could write a smart tag recognizer that only recognizes ZIP codes when they appear inside a ZIP element, or client codes when they appear inside a billTo element.

A smart tag recognizer could be written to respond when a user types the word “problem” into the body element of a letter. The context menu could allow a choice among different canned “problem letter” phrases, which would be inserted automatically into the document.

Tip

Tip

Additional information about smart tags is included in the Smart Tags SDK, which can be downloaded from the Microsoft website.

The research pane

The research pane feature of Office 2003 allows you to query a variety of research services on the Web from the task pane, without leaving your document. For example, you could look up word definitions, review stock quotes and information, perform Google searches, check the prices of goods for sale, or find journal articles related to a topic (as shown in Figure 14-6).

Using the research pane

Figure 14-6. Using the research pane

You display the research pane by clicking Research on the task pane list. You interact with it by typing your search terms into the Search for: box, selecting a research service, and clicking the green arrow icon. Alternatively, you can press Alt plus a word to query the research service automatically. The research service will return a set of results in the bottom part of the pane.

The research pane can be used with Word, Excel, PowerPoint, Outlook, Publisher and Visio. Unlike smart documents, the research pane does not require your Word documents or Excel workbooks to be associated with a schema; it is independent of the XML editing features of Office.

Available research services

A number of research services are provided with Office 2003. These include dictionaries, thesauri, encyclopedias, language translators, Web searches, and stock quotes. The complete list can be viewed by clicking Research options on the task pane, which brings up the Research Options dialog shown in Figure 14-7.

The Research Options dialog

Figure 14-7. The Research Options dialog

There are additional research services written by third parties that can be added to Office, sometimes for a fee. These services can be found on the Office Marketplace Web site, which can be accessed from the task pane by clicking Get services from Office Marketplace. To add to your available services, click Add services on the Research Options dialog and enter the URL provided by the research service documentation.

You can also write your own research service by implementing it as a SOAP Web service. While the term “research” usually implies accessing public reference sources, the research pane can be used for any information that a user might want to look up.

For example, you could develop a private company-only research service that returns background information on a customer in response to a query using its name or customer number. This would be useful to customer service representatives who are writing letters.

How it works

Research services are implemented as SOAP Web services that are specifically designed to work with the Office research pane. The Web services have two operations: Registration and Query. The Registration operation is performed when a user first adds the research service in Office. The Query operation is performed each time the user looks something up using that service.

A number of parameters may be passed to the Query operation, such as the search text, the preferred language, and the maximum number of items to retrieve. An example of an XML document that might be passed to the Web service is shown in Example 14-12.

Example 14-12. XML document sent to the Query operation

<QueryPacket xmlns="urn:Microsoft.Search.Query"
             revision="1" build="(11.0.5308)">
  <Query>
    <SupportedFormats>
      <Format revision="1">
urn:Microsoft.Search.Response.Document:Document</Format>
    </SupportedFormats>
    <Context>
      <QueryText type="STRING" language="en-us">just-in-time
widget manufacturing</QueryText>
      <LanguagePreference>en-us</LanguagePreference>
    </Context>
  </Query>
</QueryPacket>

The research service returns an XML document with the results. The results can be rendered using a variety of techniques, including rich text, images, tables, hyperlinks, horizontal lines, collapsible sections, and even smart tags. An example of the result XML is shown in Example 14-13.[8]

Example 14-13. XML returned from the Query operation

<ResponsePacket revision="1" xmlns="urn:Microsoft.Search.Response">
  <Response>
    <Range>
      <StartAt>1</StartAt>
      <Count>2</Count>
      <TotalAvailable>738</TotalAvailable>
      <Results>
        <Content xmlns="urn:Microsoft.Search.Response.Content">
          <Image source="http://www.elibrary.com/Logo.gif"/>
          <Heading collapsible="true">
            <Text>Continuous flow manufacturing...</Text>
            <P>Microwave Journal... </P>
            <Hyperlink url="http://ask.elibrary.com/login.asp...">
              <Text>Read now ...</Text>
            </Hyperlink>
          </Heading>
          <Heading collapsible="true">
            <Text>Executives try to balance...</Text>
            <!-- etc. -->
          </Heading>
        </Content>
      </Results>
    </Range>
    <Status>SUCCESS</Status>
  </Response>
</ResponsePacket>

Tip

Tip

For more information on developing custom research services, download the Office Research SDK from the Microsoft website. It provides detailed specifications for the Web services, as well as a number of examples.

XML expansion packs and manifests

An XML expansion pack is a group of related files that make up one or more related Office XML solutions. It may include schemas, XSLT stylesheets, smart tag lists, DLLs for smart documents, external files used in smart document solutions, and other types of files.

Each XML expansion pack contains a manifest: an XML document that lists all of the files in the pack, their “types” (i.e. their roles in an Office solution, such as solutionActionHandler), and their locations. In 14.1.2, “Installing a smart document solution”, on page 322, we installed a smart document solution using the manifest shown in Example 14-14.

Example 14-14. Manifest for the smart document solution (letter_manifest.xml)

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns=
"http://schemas.microsoft.com/office/xmlexpansionpacks/2003">
  <version>1.1</version>
  <updateFrequency>20160</updateFrequency>
  <uri>http://xmlinoffice.com/letter</uri>
  <solution>
    <solutionID>{15960625-1612-46AB-877C-BBCB59503FCF}</solutionID>
    <type>smartDocument</type>
    <alias lcid="*">Letter Smart Document Sample</alias>
    <file>
      <type>solutionActionHandler</type>
      <version>1.0</version>
      <filePath>LetterSample.dll</filePath>
      <CLSID>{F854ADAA-A8A2-40EC-B31A-A2DB811FD7F3}</CLSID>
    </file>
  </solution>
  <solution>
    <solutionID>schema</solutionID>
    <type>schema</type>
    <alias lcid="*">letter</alias>
    <file>
      <type>schema</type>
      <version>1.0</version>
      <filePath>letter.xsd</filePath>
    </file>
  </solution>
</manifest>
  • The manifest element defines the namespace for the manifest document.

  • The uri element contains the target namespace for the solutions.

  • The first solution element describes a solution whose type is smartDocument. It uses the file LetterSample.dll, whose type is solutionActionHandler. The alias element’s content is the name that appears in the XML Expansion Pack dialog.

  • The second solution element describes a solution whose type is schema. It uses the file letter.xsd – whose type is also schema – as its schema document. (Other valid values of type for files are transform and other.) The alias element’s content is used in the Schema Library as a nickname for the schema.

Other element types may be included in a manifest; a detailed reference is provided in the Smart Documents SDK.

When this expansion pack is installed, both the smart document solution and the schema are added to the library locally.

When an expansion pack is associated with a document, a link to the manifest is stored within the document’s metadata, which allows the solution to be centrally maintained. The manifest can be stored on a file server or in SharePoint, or even be returned by a Web service. To upgrade to the latest version of a solution, a user can select it and click Update in the XML Expansion Packs dialog.

Alternatively, a solution can be set up for automatic installations and updates. If a user who does not have the latest version of an expansion pack installed tries to open an associated document, he will be prompted to allow the expansion pack to be installed automatically. This approach greatly simplifies deployment of changes and upgrades to solutions.

XML expansion packs are not just for smart documents. They can be used simply to add schemas and stylesheets to Word documents, rather than requiring that users do this manually. XML expansion packs can also be used in Excel, by clicking XML Expansion Packs on the XML submenu.



[1] Microsoft refers to applications that use these features as smart client solutions.

[2] Microsoft provides a Smart Documents Software Development Kit (SDK), downloadable from its website, which provides further detailed information and examples.

[3] Smart document solutions can also be developed in Visual Basic .NET, C# .NET and Visual C++.

[4] The interface is in the Microsoft Smart Tags 2.0 Type Library.

[5] In Visual Basic 6.0, the property and method names are prefixed with “ISmartDocument_”.

[6] Only enough of the code is included to show how it works.

[7] For that reason, in smart document parlance an element-type name is called a namespace#element name or smart document type name.

[8] Some information is truncated for legibility.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.147.215