[ Team LiB ] |
Recipe 12.4 Perform a Mail Merge from Access to Word12.4.1 ProblemYou'd like to be able to do a mail merge to Word using Access data, without having to launch the mail merge from Word using its mail merge features. 12.4.2 SolutionAccess allows you to output data directly to any format using the DoCmd.OutputTo functionality. You can then run a mail merge from Word to a predefined Word template that contains the merge codes. First you must create the Word template that holds your merge codes; then you can write the code in Access that performs the merge. The sample application 12-04.MDB contains a table and a query that retrieves the data to be sent to Word. To perform a mail merge from Access to Word, follow these steps:
Figure 12-6. Set a reference to the Word library
12.4.3 DiscussionMicrosoft Word exposes an Application object, which you can use to launch Word, and a Document object, which you can use to open a new Word document. Once you've launched Word, you can use all its capabilities from your Access application. The following sections outline the steps involved in communicating with Word via Automation. 12.4.3.1 Starting the connection with Word for WindowsTo be able to work with Word from Access, you must create an object variable to refer to the Word Application object. You also need a Document variable to work with a specific Word document. The following code fragment defines these variables: Dim doc As Word.Document Dim wrdApp As Word.Application The next step is to delete any previously existing data source documents: strPath = FixPath(CurrentProject.Path) Kill strPath & conQuery & ".doc" If the document doesn't exist, the error handler will simply resume on the next statement and create a new document containing the data from the query using the OutputTo method of the DoCmd object: DoCmd.OutputTo acOutputQuery, conQuery, _ acFormatRTF, strPath & conQuery & ".doc", False 12.4.3.2 Performing the mail mergeTo launch Word and create a new document based on the mail merge template, set the Application object to a new instance of Word.Application. Set the Document object to create a new document using the Application's Add method, basing it on your template: Set wrdApp = New Word.Application Set doc = wrdApp.Documents.Add(strPath & conTemplate) Once the document is open, use the Document object's MailMerge method to merge the data to a new document: With doc.MailMerge .OpenDataSource Name:=strDataSource .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With If .State = wdMainAndDataSource Then .Execute End If End With In Access 2002 and later you must use the .OpenDataSource method in your code, but this isn't required in Access 2000. 12.4.3.3 Finishing the mail mergeTo display the Word documents, set the Application object's Visible property to True: wrdApp.Visible = True Once the Word document is displayed, clean up by setting the Word object variables to Nothing. This frees up the memory and system resources: Set doc = Nothing Set wrdApp = Nothing You'll see both the new document, named Document1 (based on the template), and the actual merge documents. You can save the merge documents or print them from Word. |
[ Team LiB ] |