Merging a Query to a Bookmark in a MailMerge

A

A. Smart

I have a piece of code that imports a number of fields on a form to relative
bookmarks on a word Mail Merge document. I would also like to import a query
into the document. Is this possible using bookmarks or is it better using
Merge Fields in the document.
Either way please let me know how to accomplish this. Thank you for your
help in advance.
My code is very similar to this:

Private Sub cmdMergeLetter_Click()
' © 1999 Arvin Meyer
' This code was written by Arvin Meyer
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.

On Error GoTo Err_cmdMergeLetter_Click

Dim WordTemplate As String
Dim strFullName As String
Dim objWord As Word.Application

Set objWord = CreateObject("Word.Application")

WordTemplate = Application.CurrentProject.Path & "\Letter.dot"
strFullName = Me.txtPersonFirstName & " " & Me.txtPersonLastName

With objWord
.Visible = True
.Documents.Add (WordTemplate)
.Caption = "Letter to " & strFullName & "

' If document is protected, Unprotect it.
If .ActiveDocument.ProtectionType <> wdNoProtection Then
.ActiveDocument.Unprotect Password:=""
End If

.ActiveDocument.Bookmarks("CompanyName").Select
.Selection.Text = (CStr(Me.txtCompanyName))
.ActiveDocument.Bookmarks("FirstName").Select
.Selection.Text = (CStr(Me.txtPersonFirstName))
.ActiveDocument.Bookmarks("LastName").Select
.Selection.Text = (CStr(Me.txtPersonLastName))
.ActiveDocument.Bookmarks("Address").Select
.Selection.Text = (CStr(Me.txtPersonAddress))
.ActiveDocument.Bookmarks("City").Select
.Selection.Text = (CStr(Me.txtPersonCity))
.ActiveDocument.Bookmarks("State").Select
.Selection.Text = (CStr(Me.txtPersonState))
.ActiveDocument.Bookmarks("ZipCode").Select
.Selection.Text = (CStr(Me.txtZip))
.Activate

' ReProtect the document.
If .ActiveDocument.ProtectionType = wdNoProtection Then
.ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If

.ActiveDocument.PrintOut
.ActiveDocument.Close (wdDoNotSaveChanges)

End With

Exit_cmdMergeLetter_Click:
objWord.Quit
Set objWord = Nothing
Exit Sub

Err_cmdMergeLetter_Click:
MsgBox Err.Number & ": " & Err.Description, vbInformation, "Error"
Resume Exit_cmdMergeLetter_Click

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top