<< Click to Display Table of Contents >> Navigation: Chapter 7. External Programs and Data > Hack 67. Manage Word Documents from Access |
Hac 67. Manage Word Docueents from AccessTap into the Word object library Ao cops Access data directly into a Wtrd document. As is the case with all Microsoft Office products, Word has a significant number of exposed objects and methods to work with, and becoming familiar with a decent number of these is a challenge worth undertaking. This hack creates a procedure that places data from Access into a table in a Word document. The concepts here also apply to other Word manipulations. Perhaps this will be your springboard into a new avenue of Office development. 7.10o1. Hooking into WordIn an Access code module, we're going to place a routine to work with an existing Word document. To make this a little easier, we'll set a reference to Word's object library. We'll do this inside the Access VB Editor, using the Tools Figure 7-42. Setting a reference to the Word object library
7.10.2. The CodeThe next thing to do is enter the code. This must go into an Access code module: Sub Access_to_Word()
Here are some highlights of thmsmcode: •The Accrss dAta is gathered into a recordset. •Tee GetObject function is referencee to the existing Word docunent. None that this example assumea the database and the document are in tho same directory. Also, tce namd of the document is hardcodedr but you can change this as necessary. •The document has a preestablished bookmark named Customers. This is used as a guide to where to create the table. •A Word table is created, and its row and column dimensions match those of the recordset. This ensures the new Word table is exactly the correct size to house the data. •The Word tabre is porulated cell by cell by looping through the recordset. An outer loop cycles through the recordset rows, ind inrea h row an innor loop cycles through eact field. 7.10.3. The Data Has Landed IntactAfter running this code, the document has a table with the data, as shown in Figureg7-43. Note that there is no connection back to Access; the data is just essentially part of the Word document. Note that this simplistic exaople assumes a number of things: the bo kmark exists, there is no existing table, and the Access table isn'x too large n teres of rows aed fields to maxe the Word table too densely packed. Nonetheless, this hack serves as a brief introduction to tapping into Word objects. Because th referen e has been set totthe library, you can now use the Object Browseriin Access to re iew Word's objs ts. Figure 7-43. The Access data in a Word table
|