By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,323 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,323 IT Pros & Developers. It's quick & easy.

Mail Merge VB Access to Word to Print Multiple Records Via Button

P: 1
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:\")

'Move to each bookmark and insert text from the form.
.Selection.Text = (CStr(Forms![frmPrivacy]![Title]))
.Selection.Text = (CStr(Forms![frmPrivacy]![FirstName]))
.Selection.Text = (CStr(Forms![frmPrivacy]![LastName]))
.Selection.Text = (CStr(Forms![frmPrivacy]![Address1]))
.Selection.Text = (CStr(Forms![frmPrivacy]![Address2]))
.Selection.Text = (CStr(Forms![frmPrivacy]![State]))
.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.
Do While objWord.BackgroundPrintingStatus > 0

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

'Quit Microsoft Word and release the object variable.
Set objWord = CreateObject("Word.Application")

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

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?
Sep 12 '07 #1
Share this Question
Share on Google+
1 Reply

P: 13

I've used the attached code in a database i made ages ago - i can't remember where it came from.

Expand|Select|Wrap|Line Numbers
  1. Private Sub RunMergeLetters_Click()
  4.     Dim strPath As String
  5.     Dim strDataSource As String
  6.     Dim conTemplate As String
  7.     Dim conQuery As String
  9.     Dim doc As Word.Document
  10.     Dim wrdApp As Word.Application
  12.     conTemplate = "Confirmationletter.doc"
  13.     conQuery = "qry 2-1 Letter Query"
  15.     On Error GoTo HandleErrors
  16.     ' Delete the rtf file, if it already exists.
  17.     strPath = "C:\My Computer\<file path>\"
  18.     strDataSource = strPath & conQuery & ".doc"
  19.     Kill strDataSource
  21.     ' Export the data to rtf format.
  22.     DoCmd.OutputTo acOutputQuery, conQuery, _
  23.      acFormatRTF, strDataSource, False
  25.     ' Start Word using the mail merge template.
  26.     Set wrdApp = New Word.Application
  27.     Set doc = wrdApp.Documents.Add(strPath & conTemplate)
  29.     ' Do the mail merge to a new document.
  30.     With doc.MailMerge
  31.         .OpenDataSource Name:=strDataSource
  32.         .Destination = wdSendToNewDocument
  33.         .SuppressBlankLines = True
  34.         With .DataSource
  35.             .FirstRecord = wdDefaultFirstRecord
  36.             .LastRecord = wdDefaultLastRecord
  37.         End With
  38.         If .State = wdMainAndDataSource Then
  39.             .Execute
  40.         End If
  41.     End With
  43.     ' Display the mail merge document.
  44.     wrdApp.Visible = True
  47. ExitHere:
  48.     Set doc = Nothing
  49.     Set wrdApp = Nothing
  50.     Set doc2 = Nothing
  51.     Set wrdApp2 = Nothing
  52.     Exit Sub
  54. HandleErrors:
  55.     Select Case Err.Number
  56.         Case 53         ' File not found.
  57.             Resume Next
  58.         Case Else
  59.             MsgBox Err.Number & ": " & Err.Description
  60.             Resume ExitHere
  61.     End Select
  63.         End If
  65. End Sub

This code merges all records in a query to a merge letter, provided the merge letter has the correct fields set up.

I don't know vb code well enough to contribute much, but I hope this helps.
Dec 18 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.