I am currently trying to do a multiple record mail merge through a query via a command button on a form. The query basically displays customers who have said yes to privacy. The user inputs a date into txtDate on frmDate and the clicks "View Queried Privacy Records" Once they click this it opens the query form frmPrivacy which then has a command button cmdMailmerge to perform the mail merge and print the records that result from that query. (I would prefer to perform the query and mail merge/print without having to go frmPrivacy)
The command button has the following code:
Private Sub cmdMailmerge_Click()
On Error GoTo MergeButton_Err
'Start Microsoft Word
Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
.Visible = True
'Open the document.
.Documents.Open ("H:\privacymerge.dot")
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("Title").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![Title]))
.ActiveDocument.Bookmarks("FirstName").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![FirstName]))
.ActiveDocument.Bookmarks("LastName").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![LastName]))
.ActiveDocument.Bookmarks("Address1").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![Address1]))
.ActiveDocument.Bookmarks("Address2").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![Address2]))
.ActiveDocument.Bookmarks("State").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![State]))
.ActiveDocument.Bookmarks("PostCode").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![Postcode]))
End With
'Print the document in the foreground so Microsoft Word will not close
'until the document finishes printing.
objWord.ActiveDocument.PrintOut
Do While objWord.BackgroundPrintingStatus > 0
Loop
'Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Quit Microsoft Word and release the object variable.
objWord.Quit
Set objWord = CreateObject("Word.Application")
Exit Sub
MergeButton_Err:
'If a field on the form is empty, remove the bookmark text, and
'continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
Exit Sub
End Sub
The above code only mail merges and prints the record that the form is currently on, so one record at a time. I need the mail merge to print all records when the query is run. I was thinking of doing a loop with the number of records so it prints all records but have no idea of how to go about it. Any ideas or suggestions?