As you discovered in the preceding section, Excel exposes many objects. You can manipulate each of these objects separately, using Excel's own properties and methods. Prior to Office 97, this was not true for Word, because Word exposed only one object, called Word.Basic. Microsoft Word 97, and versions subsequent to it, all sport the Visual Basic for Applications language. These newer versions of Word expose many objects just as Excel and other Microsoft products do.
Just as with Excel, you can use the Dim statement or Dim as New statement to launch Word. Like Excel, Word launches as a hidden object. The Word application object has a Visible property, which makes the Word object visible. If you create a Word object using automation, Word will not automatically terminate, even if the object variable is destroyed.
Figure 22.8 shows the form called frmMergeToWord, which shows the results of running a query called qryMailMerge. After the user clicks the Merge to Word command button, all the records displayed are sent to a Word mail merge and printed. Figure 22.9 shows an example of the resulting document, and Listing 22.8 shows the code that generated this document.
The code begins by presenting an hourglass mouse pointer to the user. This helps to ensure that, if the process takes a while, the user knows that something is happening. It then calls the CreateWordObj routine to create a Word object. The CreateWordObj routine is similar to the CreateExcel routine shown earlier in the chapter. The code executes the Open method on the Documents collection of the Word object. It opens a document called customerletter in the current folder. The customerletter document already has been set up to do a mail merge with the results of a query called qryMerge. The subroutine sets the Destination property of the MailMerge object to a new document. It sets the SuppressBlankLines property to True, and then executes the mail merge with the Execute method. This merges the results of qryMailMerge and creates a new document with the mail-merged letters. The PrintPreview method is executed on the ActiveDocument object so that the merged document is printed. Finally, the Visible property of the Word object is set to True, making Word visible, and the hourglass vanishes.
Although in most ways Access is a phenomenal report writer, it does have its limitations. For example, you cannot bold or italicize an individual word or phrase within a text box. This is quite limiting if you need to emphasize something such as a past due amount in a dunning letter. When the document I need to produce appears more like a letter than a report, I often think of Microsoft Word. The document pictured in Figure 22.10 produces a letter that provides information to the recipient of an order. The code shown in Listing 22.9 produces the letter.
The example first launches Word. It then gets a reference to a new document based on the Order.dot template. After that, it populates bookmarks in the document with values from the currently displayed order.