This bit of code is equivalent to setting the doc1 variable with the outcome of the Documents.Add method, as the second line of code in Listing 1 does.
The Document object's programming interface features all the functions that are available in Word's File and Edit menus. To print a document, you use the Document object's PrintOut method; to save a document to a different location, use the object's SaveAs method. When you use Word interactively, both of these methods present a dialog box that lets you choose the printer and the path. When you use the PrintOut or the SaveAs method, you can specify the printer or the file path as an argument, as the following code shows:
doc.SaveAs _
"C:\data\newinvoice.doc"
When you specify the path as an argument, no dialog box appears.
The SaveAs method also lets you specify an optional argument to indicate the output format (e.g., Word Document, Rich Text Format—RTF—Word 6.0, Text Only). The following code snippet shows how to save a document as an RTF file:
Const wdFormatRTF = 6
docn.SaveAs _
"C:\data\newinvoice.doc", _
wdFormatRTF
For more information about the constants that map to various file formats, refer to the Word documentation.
To close an open document, you use the Close method:
doc1.Close
When you close a document, all subsequently opened documents automatically move up one position in the collection. So, if you use the code
Set doc1 = word.Documents(0)
Set doc2 = word.Documents(1)
and later close the doc1 reference, doc2's position in the array becomes Documents(0).
When you work with Word documents programmatically, you can assign formats and styles to paragraphs, words, and characters in the document as you can when you use Word interactively. In addition, you can add comments and bookmarks to document elements. When you create template-based Word documents programmatically, you use bookmarks to insert user-entered data in the proper place in the Word document.
Using Document Bookmarks
A bookmark is an association between a name and a specific point in a Word document. Bookmarks are a feature of Word that you can use to mark selected text, graphics, tables, or any other element of a document. Word stores bookmarks in the document. To add a bookmark interactively, you select the text or item that you want to bookmark, then select Bookmark from the Insert menu. In the Bookmark window, you name the bookmark that you're creating.
For example, suppose you have a Word template (a .dot file) and want it to show the current date in a certain position. You open the template and select the area reserved for the date. To facilitate future editing of the template, you can put descriptive placeholder text (e.g., "Put invoice date here") in that area. After typing the placeholder text, select the text; click Insert, Bookmark; type a name for the bookmark (e.g., InvoiceDate); then save the template.
After you programmatically create a new document based on a template, you use the document's Bookmarks collection to identify and customize bookmarked text. For example, to enter a date at the InvoiceDate bookmark within an invoice document, you'd use code such as
Set dateBookMark = _
doc.Bookmarks("InvoiceDate")
dateBookMark.Range.Text = _
"7/1/2002"
As this code snippet shows, you access a bookmark by its name. The bookmark name points to the document's Bookmarks collection to get a bookmark object. The bookmark object's Range property is an instance of the TextRange type. This property individuates the bookmark's area of text. The Text property of the TextRange object lets you read and write the bookmark's current value. The preceding code snippet sets the date of the invoice to 7/1/2002.
Creating an Invoice
After you programmatically create a template-based document, you use bookmarks to populate the invoice with fresh data, then you save the document to a new file. At that point, you can print the document, email it to someone, or do anything else with it that you can do with a Word document.
Listing 2 shows CreateInvoice.vbs, which collects some information and prepares an invoice. The code opens the TestInvoice.dot template that's located in the same folder as the script, then creates a new document based on the template (you can download TestInvoice.dot from http://www.winscriptingsolutions.com, InstantDoc ID 26972). Web Figure 1 (http://www.winscriptingsolutions.com, InstantDoc ID 26972) shows the TestInvoice.dot template. (You can also download this template from the Code Library on the Windows Scripting Solutions Web site.)
CreateInvoice.vbs prompts the user for the invoice's number, date, and recipient. The code at callout A in Listing 2 uses bookmarks to merge this information with the underlying document. The code at callout B then uses the SaveAs method to save the document in the current folder. CreateInvoice.vbs creates a name for the completed document by concatenating the invoice number with the "inv_" prefix. The naming convention and the destination folder are arbitrary; you can easily change them to suit your needs. Finally, CreateInvoice.vbs closes the document and quits the application.
Prev. page
1
[2]
3
next page