Hack #67. Manage Word Documents from Access

Tap into the Word object library to copy Access data directly into a Word 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.

Hooking into Word

In 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 → References menu and the References dialog box, as shown in Figure 7-42. Note that your version number of the Word library might differ, so use whatever you have.

Setting a reference to the Word object library

Figure 7-42. Setting a reference to the Word object library

The Code

The next thing to do is enter the code. This must go into an Access code module:

Sub Access_to_Word() Dim conn As ADODB.Connection Set conn = CurrentProject.Connection Dim recset As ADODB.Recordset Set recset = New ADODB.Recordset Dim row_num As Integer Dim col_num As Integer Dim word_doc As Object 'Assumes Word doc is in same path - change name and ...

Get Access Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.