VB and Word

I’ve got a "template document - in this case represented by
“yourDocument.doc” where all sections that need to be filled in are
represented by “<<data>>”. After inserting all data from the recordset, the
“template” is “saved as” a new document.


Private oWordDoc
Private oWord As Word.Application

Dim oFind As Word.Find
Dim rv
sDoc = App.Path & "\yourDocument.doc"
Set oWord = New Word.Application
Set oWordDoc = oWord.Documents.Open(sDoc)

sSQL="your database query"

open your recordSet

Call wordReplace("<<mTime>>", rs("yourTime")
Call wordReplace("<<mPhone>>", rs("yourPhone")
Call wordReplace("<<mName>>", rs("yourName")

' print a copy
oWord.ActiveDocument.PrintOut

oWord.ActiveDocument.SaveAs FileName:=App.Path & "
ewDoc.doc"
oWordDoc.Close
Set oWordDoc = Nothing
oWord.Quit
Set oWord = Nothing

Private Sub wordReplace(s As String, r As String)
Set oFind = oWordDoc.Content.Find
oFind.Execute findText:=s, ReplaceWith:=r, Forward:=True
End Sub

Automating Microsoft Word from within Access
By automating Microsoft Word, I mean I want to control Word from within Access and populate a document with data pulled from the Access database. There are several methods of doing this; I chose this one because the client wanted the processes to be driven from within Access. He wanted to click a button on one of the application’s forms and open an already created Word template and have the document populated with data from the current record. This is a four-step process:

  1. Open Microsoft Word (if it is not already running).
  2. Create a new document based on a template that has several bookmarks defined.
  3. Copy data from the current Access record to the corresponding bookmark.
  4. Preview the document using Word’s Print Preview mode.

To create a template in Word, create a new blank document, enter the text you need, and save the document as a Document Template. Add bookmarks at any of the locations where data from Access will be inserted. To add a bookmark, position the cursor at the appropriate location and choose Insert | Bookmark from the main menu. The dialog box that will open will show all the currently defined bookmarks and allow you to change them, delete them, or add a new one. For this client, most of the bookmarks were for fields such as Employee First and Last Name, Employee ID, Department, and Contact Information.

Microsoft Word is opened from within Access as an automation server. This means creating an instance of Word that will run in the background and having a reference within an Access module that Access can use to manipulate the Word instance.

The code to open an instance of Word and load the template named EmpForm.dot is as follows:
‘dim a variable to reference the Word instance
Dim objWord as Word.Application

‘start Word
Set objWord = New Word.Application

‘open the template – assumes the template is in
the same folder as the Access application
objWord.Documents.Add “EmpForm.dot”

‘make Word visible so the user can preview the document
objWord.Visible

The next step is to update the bookmarks with values from the current record. If the bookmark is named EmpID and the Access form and field are named frmMain.EmpID, the code to load the Access data into Word is:

objWord.ActiveDocument.Bookmarks.Item(“EmpID”).Range.Text = frmMain.EmpID

Bookmark objects implement a Range method that returns a reference to a text range. In the template I described above, this range is a simple insertion point. It can also span several blocks of text and other objects. This can be used to update entire sections of the template with a single command.

The final step is to execute the PrintPreview, using this command:

objWord.ActiveDocument.PrintPreview

Windows MSDN Link