Advertisment

Smart Documents in Office 2003  

author-image
PCQ Bureau
New Update

One of the biggest things touted about MS Office 2003 was that it was XML compliant. You could save all your documents as XML files, and use Office 2003 to communicate with other XML compliant applications. While it sounds great, how do you actually use the XML capabilities of Office 2003? We'll demonstrate that in this article, using a feature of Word 2003 called Smart Documents. This is a Word document that uses XML for data entry. What goes inside the XML code of the document can be created using development tools such as Visual Studio. You can use this feature in a number of ways, such as automatically entering data, accessing external data and placing it in a particular place in the document and making it sharable across other applications. Take a law firm for instance. A lot of the documents generated by a law firm have most of the content pre-defined. It's only certain parts of the document that actually need to be changed. In this article, we will talk about how you can ease this process using the smart document feature. However, before you venture out to create a smart document, you should possess a good knowledge of XML and programming languages such as VB, C++ and C#.

Advertisment
Direct

Hit!
Applies to:

Organizations generating lots of redundant documents
USP:

Automatically enters data in documents using XML tags
Links:

http://msdn.microsoft.com/library/default.asp?



url=/library/en-us/odc_wd2003_ta/html/odc_


smartdocovw.asp

The first step for creating a smart document is to create a regular Word file, where only certain parts of the document need to be changed. For instance, going back to the law firm example, when filing a case in the court, you have to submit a case summary along with an application. Here, the application content would remain the same, while the case summary would change for different parties. A law firm could benefit from smart documents by creating a single document where the application content remains the same, while the case summary changes for different clients. Here, the case summary would be created using

XML. The XML file, also called the schema, contains a database of the different elements of a document that can be applied on the smart document. You can even enhance the solution by integrating controls such as list boxes, check boxes and option buttons to restrict users to select only from the predetermined values. Let's see how to create an XML schema.

Create an XML schema



XML schema is a database file that contains XML tagged data, which is used in the document to apply logic in the document's content. Here is an XML schema for a case-filing document:










targetNamespace="urn:schemas-microsoft-com.office11demos.WordProductsSD"

elementFormDefault="qualified"



attributeFormDefault="unqualified" id="Contract">


























































Advertisment

You can type it in Word or Notepad, and save it with the .xsd extension. If you don't know

XML, you can use VS.NET to create it easily.

Attach the XML schema to a document



To attach the XML schema to your document, click on Tools in the menu bar and then select 'Templates and add-ins'. This will open a Templates selection window. Here select the XML Schema, then click on the Add Schema button and open the

.xsd file you just created, and click on Ok to finish.

Once you have attached the schema, the document's elements are defined by the XML tag, so that the data within any given XML element can be manipulated intelligently. For example, the text that falls within the case code element will be described by the schema in the document as 'Case code' . The schema can also retrieve the data and place it into another database or application. After attaching the schema, you will find an XML structure window panel on the right side of the Word document. Down in the same window, you will find the XML elements that

Advertisment

you have created in the schema file. Choose the XML elements from this list that you want to add in the

document.

Create a DLL file to use the smart



document or tag API



You have to use the Smart Document API (Application Program Interface) and the Smart Document XML file schema to write the code, which will implement IsmartDocument (an API). Based on the XML tag on the document, this DLL file displays content in the Document Actions task panel. It also takes action when the user interacts with the document using controls such as the text box and combo box.

To create the dll file, you can use any language from VS.NET. Here is an example to create one. Open a new project in VS.NET and then open a VB project. Now, from the list of project template select 'class library' and give any name to the project. We called it 'contract'. Then click on Ok. This will open a property sheet for the project on the right side. In the property sheet you will find an object called class1.vb. This is the default class name of the project. You can rename this default class by right clicking on it. On the left side of the property sheet, you will find a code window displaying the code, which will be something like this.

Advertisment

Public Class Class1



End Class

To rename, edit Class1. After this, let's add the API by clicking on Project>Add References; this will open a selection window where you need to select the smart tags components. Here, select the Com tab and select 'Microsoft Smart Tag 2.0' from the list. Then click on Select and then on Ok to add the component to the project. Again, come to the same code window and add the following lines.

Imports Microsoft.Office.Interop.SmartTag



Public Class Class1


Implements Microsoft.Office.Interop.SmartTag.ISmartDocument


End Class

Advertisment

Creating a .dll file using Visual Studio

Now, suppose you want to add the control on the document. For example, if you want that the user should get help while entering case code, then in the code window, you need to add a property called ControlCaptionFromID, which specifies the caption displayed in the Document Actions task panel for each control in the Smart Document. Add the following mentioned lines in the code window.

Const cNAMESPACE As String = "contract"



Const cTEXTBOX As String = cNAMESPACE & "#casecode"

Advertisment

You can add control by using cTEXTBOX variable, which is mapped to the XML element 'casecode'. For example, to control the document, the code will be this.

Public ReadOnly Property ControlID(ByVal XMLTypeName As String, ByVal ControlIndex As Integer) _



As Integer Implements Microsoft.Office.Interop.SmartTag.ISmartDocument.ControlID


Get


Select Case XMLTypeName


Case cTEXTBOX


ControlID = ControlIndex


End Select


End Get


End Property


Public ReadOnly Property ControlCaptionFromID(ByVal ControlID As Integer, _


ByVal ApplicationName As String, ByVal LocaleID As Integer, ByVal As String, _


ByVal Xml As String, ByVal Target As Object) As String Implements Microsoft.Office. _


Interop.SmartTag.ISmartDocument.ControlCaptionFromID


Get


Select Case ControlID


Case 1


ControlCaptionFromID = _


"Please new case code:"


Case Else


End Select


End Get


End Property



















Save this project file (contract) and compile it. After compiling you will get contract.dll in C:\Documents and Settings\user\My Documents\Visual Studio Projects\contect\bin. Copy all the files from here and save it to the location where you have created the document and the schema file.

Advertisment

Define an XML expansion pack manifest file



Finally, you have to define the manifest file that describes the property and behavior of the document. This is also an XML file, which contains the name and path of schema and the DLL file. To define an XML expansion pack manifest file, open Notepad and write the following code.

1.1



20160


Contract


-


contract.contarctclass


smartDocument


Contact - VB.NET


-


solutionActionHandler


1.0


bin\contarct.dll


contract.contarctclass 


-


schema


schema


Simple Smart Document Sample Schema


-


schema


1.0


contract.xsd

















Save the file with a name, say manifest.xml. To attach this file to the document, click on Tools in the menu bar and select 'Templates and add-ins'. Now select the XML Extension Pack. Click on Add and then on Ok. Here, you will get a dialog box. Click on No. If you face any problems while adding the manifest file to the extension pack, run 'Disable Extension Manifest Security tool' (available on this month's Essential CD). With this done, you have created the Smart Document.

Sanjay Majumder

Advertisment