Datasheet
674
Merging Data with a Word Document
For this example, we created three bookmarks: CurrentDate,
AccessAddress, and AccessSalutation. We then saved the .dotx file in
the same folder as our database.
Writing the merge code
Now that the Word template is ready to go, we can create another button
on the Customer form that sends data from the form into Word, as shown in
Figure 1-6.
Figure 1-6:
Adding
another
button to
send data to
Word.
When the button is on the form, you can add this code to this button’s
Click event procedure to send the data to the Word document:
Private Sub cmdWord_Click()
‘Declare Variables
Dim sAccessAddress As String
Dim sAccessSalutation As String
‘Build sAccessAddress
sAccessAddress = FirstName & “ “ & LastName & _
vbCrLf & Company & vbCrLf & Address1 & _
vbCrLf & City & “, “ & StateProv & “ “ & ZIPCode
‘Build sAccessSalutation
sAccessSalutation = FirstName & “ “ & LastName
‘Declare and set Word object variables
Dim Wrd As New Word.Application
Set Wrd = CreateObject(“Word.Application”)
‘Specify Path to Template
Dim sMergeDoc As String
sMergeDoc = Application.CurrentProject.Path & _
“\WordMergeDocument.dotx”
44_532188-bk09ch01.indd 67444_532188-bk09ch01.indd 674 3/29/10 10:59 PM3/29/10 10:59 PM










