Automating Access to Word With Bookmarks- Even Example Won't Work! (1 Viewer)


Registered User.
Local time
Today, 04:30
Apr 12, 2012
I'm a complete Access programming newbie. I have used Databases before but this is the first time of trying to do some really cool stuff. Essentially i'd like to merge Access field names into a Word document with bookmarks. I'm using Access and Word 2003. Eventually i'd like to attach this to a form button to automate the creation of new letters. Now i've tried some stuff with not much success so i tried using an example from the Micosoft support pages. It involved starting a new document, adding bookmarks to it then adding a command button to a form in the existing sample 'Northwind' database and using the following code as an event procedure (on click):

Private Sub MergeButton_Click()
On Error GoTo MergeButton_Err

Dim objWord As Word.Application

'Copy the Photo control on the Employees form.
DoCmd.GoToControl "Photo"
DoCmd.RunCommand acCmdCopy

'Start Microsoft Word 97.
Set objWord = CreateObject("Word.Application")

With objWord
'Make the application visible.
.Visible = True

'Open the document.
.Documents.Open ("C:\MyMerge.doc")

'Move to each bookmark and insert text from the form.
.Selection.Text = (CStr(Forms!Employees!FirstName))
.Selection.Text = (CStr(Forms!Employees!LastName))
.Selection.Text = (CStr(Forms!Employees!Address))
.Selection.Text = (CStr(Forms!Employees!City))
.Selection.Text = (CStr(Forms!Employees!Region))
.Selection.Text = (CStr(Forms!Employees!PostalCode))
.Selection.Text = (CStr(Forms!Employees!FirstName))

'Paste the photo.
End With

'Print the document in the foreground so Microsoft Word will not close
'until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False

'Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

'Quit Microsoft Word and release the object variable.
Set objWord = Nothing
Exit Sub

'If a field on the form is empty, remove the bookmark text, and
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next

'If the Photo field is empty.
ElseIf Err.Number = 2046 Then
MsgBox "Please add a photo to this record and try again."
MsgBox Err.Number & vbCr & Err.Description
End If

Exit Sub
End Sub

Now, i must be doing really daft if i can't even get this to work!! On clicking the command button i get an error box saying 'compile error: User defined type not defined'. When i click OK, Private Sub MergeButton_Click() is highlighted in yellow and objWord As Word.Application just highlighted in blue. I've read similar posts which suggest to make sure i reference the object library, which i think i've done (Tools>References>Microsoft Word 11.0 Object Library) and is selected. Help!

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:30
Feb 19, 2002
You will need to fix the compile error. Open up the references again and make sure the current version of Word is seleted. Also look for any marked MISSING and either remove the code that caused you to add the reference or fix the reference by installing the correct .dll.

The following is several code snippets from one of my applications. It uses a variety of Word objects beyond simple bookmarks. Some of the code takes a specific field and posts it to a specific bookmark but the application is much more sophisticated and in fact the user can create his own Word documents and then map table fields to specific bookmarks so that the associations are not all hardcodes as are the first several examples. The last example shows a loop that reads through the defined bookmarks for a given document and posts a field to a bookmark based on the defined relationship.

Public WordApp As Word.Application
Public WordDoc As Word.Document

WordDoc.FormFields("CurrentDate").Result = Format(frm.txtTodaysDate, "mmmm dd, yyyy")
WordDoc.FormFields("ReturnDate").Result = Format(frm.txtReturnDate, "mmmm dd, yyyy")
    If IsNull(frm.txtCustomText) Then
        WordDoc.Bookmarks("HRComment").Range.Text = ""
        WordDoc.Bookmarks("HRComment").Range.Text = frm.txtCustomText
    End If
    Do While rsFields.EOF = False
        Select Case rsFields!FieldType
            Case "Memo"
                 WordDoc.Bookmarks(rsFields!BookMarkName).Range.Fields(1).Result.Text = rsData.Fields(rsFields!FieldDescription)
            Case "Ckbox"
                WordDoc.FormFields(rsFields!BookMarkName).CheckBox.Value = rsData.Fields(rsFields!FieldDescription)
            Case "Date"
                 WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "mmmm dd, yyyy")
             Case "Phone"
                WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "\(###\) 000\-0000")
            Case Else
                WordDoc.FormFields(rsFields!BookMarkName).Result = rsData.Fields(rsFields!FieldDescription)
        End Select


Registered User.
Local time
Today, 04:30
Apr 12, 2012
That's fantastic, thank you very much. Once I selected the object libraries in both Word and Access it worked a treat.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom