Microsoft Office Tutorials and References
In Depth Information
Merging Data with a Word Document
Wrd.Documents.Add sMergeDoc
Wrd.Visible = True
‘Replace Bookmarks with Values
With Wrd.ActiveDocument.Bookmarks
.Item(“CurrentDate”).Range.Text = Date
.Item(“AccessAddress”).Range.Text = sAccessAddress
.Item(“AccessSalutation”).Range.Text = sAccessSalutation
End With
‘Open in Print Preview mode, let user print
‘Clean Up code
Set Wrd = Nothing
End Sub
Again, this example has a lot of code, but that code breaks down into several
sections. It declares the variables you’re going to use, sets the address and
salutation variables, opens Word (using the template you created), replaces
the bookmarks with values from Access, and shows the Print Preview view
for the document. The following section takes a closer look at some key
components of this code.
Examining the merge code
After declaring the string values, you set the sAccessAddress variable to
a concatenated string of values from the form. You use the line-continuation
character (an underscore) as well as the vbCrLf keyword, which starts a
new line in the string variable, as follows:
‘Build sAccessAddress
sAccessAddress = FirstName & “ “ & LastName & _
vbCrLf & Company & vbCrLf & Address1 & _
vbCrLf & City & “, “ & StateProv & “ “ & ZIPCode
You also build the sAccessSalutation variable by combining the
firstname and last-name fields from the form, with a space in between:
‘Build sAccessSalutation
sAccessSalutation = FirstName & “ “ & LastName
Search JabSto ::

Custom Search