• subscribe
November 05, 2002 12:00 AM

Creating Word Documents Programmatically

VBScripting Solutions
Windows IT Pro
InstantDoc ID #26972
Downloads
26972.zip

Using a script to create a text file is fairly easy because the Windows environment in general and the Windows Script Host (WSH) environment in particular provide ad hoc tools for working with text files. However, although text files are ideal for storing simple, unstructured information, they're inadequate when your requirements are more sophisticated. If you need to present hierarchical information, you can rely on XML and create tagged text with or without referencing a schema. Frequently, though, what you'd like to be able to do is create documents that present informative content in a complex layout. HTML files are one alternative for marrying content and sophisticated layout. However, HTML documents are plain text and, as such, are subject to tampering. Furthermore, HTML documents are rarely self-contained, often linking to external resources such as images, ActiveX controls, and applets.

In contrast, Microsoft Word is a de facto standard for real-world document exchange. Creating such documents interactively is easy, but can you automate that process by creating and programmatically using template-based documents? The answer is certainly yes, as I explain. I don't go into detail about the Word end of the process; rather, I concentrate on creating and manipulating Word documents programmatically and assume that you know the basics of Word templates and bookmarks.

Using Word Templates
Sometimes (although not as often as I'd like), I need to prepare invoices, then print them for mailing or distribute them through email. Typically, I open Word, select my invoice template, enter the necessary information, and save the file as a new document. Then, I send the invoice to the client either through snail mail or as an electronic document attached to an email message.

In most cases, invoices and other forms are simply documents that share the same template but contain different information, such as date, invoice number, item description, and billing amount. The template on which the document is based typically contains one or more blank fields in which users can insert data specific to that particular form.

In Word, you can create forms for standard documents such as invoices by defining a .dot template. When you want to prepare a new document based on a template, you click File, New. After you select the template you want from the New window and click OK, Word creates and displays a new form based on that template. On the blank form, you enter variable information where necessary.

When you use Word interactively, you can see where you need to provide information. But how can you make a WSH script enter variable information in the correct fields? Before I answer that question, let's review the Word object model—that is, the COM-based infrastructure that lets you programmatically control Word's behavior.

The Word Object Model
Word's object model includes a rich and powerful hierarchy of objects. This object model exposes many of Word's interactive functions programmatically to COM-aware clients (i.e., WSH and Visual Basic—VB—scripts). The root element in Word's object model is the Word.Application object. To create an instance of this object, use the following code:

Dim word
Set word = CreateObject _
  ("Word.Application")

Word's object model is implemented within the Word executable (winword.exe), not within a separate DLL. Thus, each instance of the Word.Application object is equivalent to having an instance of the Word application up and running. However, those instances are invisible. Because you can't see them, you run the risk of proliferating instances of Word that exist without doing anything, thus wasting system resources. To avoid proliferating useless instances of Word, your scripts should always quit the object when they're done with it by using the Application object's Quit method:

word.Quit

Although newly created instances of Word are invisible by default, you can make them visible by setting the Visible property to True:

word.Visible = True

When an instance is visible, you can also close the application manually. Setting the Visible property to True can be very useful when you're debugging scripted Word applications.

Working with members of Word's object model is the same as working with the application interactively. After you initialize the application, you create a new document or open an existing one. To create a new, empty document based on a specific template, you call the Add method on Word's Documents collection and specify the .dot file for the template you want to use. The following code creates a document that contains the features built into a template called Invoice:

Set doc = _
  word.Documents.Add("invoice.dot")

To open an existing document, you use the Documents collection, which contains Document objects. The code in Listing 1 opens existing documents named invoice.doc and expenses.doc. This code snippet opens invoice.doc first, then opens expenses.doc. At this point, the status of the programmatically created document is exactly the same as that of a template document that you've opened interactively by clicking File, Open from within Word. If you made the instance visible, you would see document windows for invoice.doc and expenses.doc.

The doc1 and doc2 variables in the listing reference objects that represent the newly opened Word documents. You can also use an index to the Documents collection to obtain references to open documents. For example, the following code returns a reference to the first document that's been opened:

Set doc1 = word.Documents(0)


ARTICLE TOOLS

Comments
  • Henry
    6 years ago
    Apr 11, 2006

    very good documentation

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...