Microsoft Office Professional Developer Portal
 Print Print
Average Rating: 4.8/5 | Rate this item | 5 users have rated this item.
Designing Smart Documents in Office 2003 (cont'd)
Initializing the Document
The CustomerDocument.vb file in your CustomerManagement.sln project contains the .NET implementation of the SmartDocInitialize method.


   Public Sub SmartDocInitialize( _
      ByVal ApplicationName As String, _
      ByVal Document As Object, _
      ByVal SolutionPath As String, _
      ByVal SolutionRegKeyRoot As String) Implements _ 
         Microsoft.Office.Interop.SmartTag. _
         ISmartDocument.SmartDocInitialize
   
      Dim log As New EventLog
      log.Source = "Customer smart Document"
      Document = New DocumentUtils(Document)
      log.WriteEntry("Smart document initialized")
      log.Dispose()
   End Sub
The method creates a DocumentUtils instance which takes an Object (an Office Document object) instance as an argument. The method takes an Object because the Document object varies based on which Office application you're automating, e.g. Word or Excel. From a design perspective it's important to keep a pointer to this object, because the document context is required for both this method and for onPaneUpdateComplete.

Here's the DocumentUtils constructor.

   Private types As Hashtable

   Public Sub New(ByVal document As Object)
      ' add nodename, identifier...this can be 
      ' configured via XML, which is 
      ' actually the best way to implement.
      types = New Hashtable
      InitializeType(Constants.DOCUMENT_ROOT)
      InitializeType(Constants.REGION)
      InitializeType(Constants.ITEM_NAME)
      Me.document = CType(document, Word.Document)
   End Sub
In the constructor shown above, you would initialize all the XmlTypes or SmartNodes. Based on the schema definition there are three important nodes:

  • Root Node—the documentRoot in this case
  • Region—which displays a list of allowable regions
  • Itemname—this node may contain only valid items available in the inventory.
The constructor initializes these three nodes and stores them in the types hashtable using the InitializeType method. The InitializeType snippet shown below accepts an XMLType name as an identifier for the XmlTypes or SmartNodes collection.

   Private Sub InitializeType(ByVal typeName As String)
      Dim smartNode As SmartType
      Dim control As ISmartControl
      'all this could be configuration driven
      Select Case typeName
      Case Constants.DOCUMENT_ROOT
         'even this can be xml configured
         control = New DocumentRoot
         smartNode = New SmartType(typeName, _
            "Root content", _       
            Constants.DOCUMENT_ROOT_TYPE_ID, control)
      ...
      End Select
      types.Add(Constants.DOCUMENT_NAMESPACE + 
         Constants.HASH_SYMBOL + typeName, smartNode)
   End Sub
 
Figure 2. Implementation of the ISmartControl and SmartType: The figure shows the overall architecture for the relationship between a SmartType, the abstract class AbstractSmartControl and the ISmartControl interface.
You create each XmlType as a SmartType instance that has a reference to a specialized class implementation that contains the implementation for the Document actions pane via the ISmartControl interface.

An ISmartControl is defined as seen in the SmartControls.vb file. You create one concrete implementation of ISmartControl for each XmlType. In the preceding snippet, you'll see references to the concrete implementation for the documentRoot XMLType which is called "DocumentRoot." The Region and Item implement this ISmartControl via an abstract class called "AbstractSmartControl." The abstract class provides the flexibility to use common base services, if required. The abstract definitions are just overridable methods.

You need a SmartType constructor which will have an associated instance of ISmartControl. Figure 2 shows the overall architecture.

   Public Sub New(ByVal Name As String, _
      ByVal caption As String, _
      ByVal TypeId As Integer, _
      ByVal smartControl As ISmartControl)
      
      TypeName = Name
      TypeCaption = caption
      Id = TypeId
      Control = smartControl
   End Sub
 
Figure 3. Implementation Architecture: When the DocumentUtils constructor completes, you have three SmartTypes stored in a Hashtable collection.
At runtime you create a SmartType instance by passing the XMLTypeName, XMLTypeCaption, and XMLTypeID with the ISmartControl instance. At this point the class, with its ISmartControl instance reference, will be very similar to the concrete definitions of the ISmartDocument. Note that the DocumentUtils constructor adds the XMLType, and SmartType instance to the Hashtable collection as follows.

   types.Add(Constants.DOCUMENT_NAMESPACE + _
      Constants.HASH_SYMBOL + typeName, smartNode)
Each type in a SmartDocument is a combination of "NAMESPACE#TYPENAME," for example, http://Officesamples/Customer/2004#documentRoot. For this sample application, when the constructor completes, you have three XMLtypes defined in the collection. Figure 3 shows an overall view.

Previous Page: Writing SmartDocument Managed Code Next Page: Implementing XMLType Definitions


Page 1: IntroductionPage 7: Implementing Control Definitions
Page 2: Getting StartedPage 8: Rendering and Populating Controls
Page 3: Applying SchemaPage 9: Populating ComboBoxes in the Itemname and Region Nodes
Page 4: Writing SmartDocument Managed CodePage 10: Reading Values from SmartDocuments
Page 5: Initializing the DocumentPage 11: Creating a Manifest
Page 6: Implementing XMLType Definitions