By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,493 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,493 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:\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?
Sep 12 '07 #1
Share this Question
Share on Google+
1 Reply


P: 13
Hi,

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()
  2.  
  3.  
  4.     Dim strPath As String
  5.     Dim strDataSource As String
  6.     Dim conTemplate As String
  7.     Dim conQuery As String
  8.  
  9.     Dim doc As Word.Document
  10.     Dim wrdApp As Word.Application
  11.  
  12.     conTemplate = "Confirmationletter.doc"
  13.     conQuery = "qry 2-1 Letter Query"
  14.  
  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
  20.  
  21.     ' Export the data to rtf format.
  22.     DoCmd.OutputTo acOutputQuery, conQuery, _
  23.      acFormatRTF, strDataSource, False
  24.  
  25.     ' Start Word using the mail merge template.
  26.     Set wrdApp = New Word.Application
  27.     Set doc = wrdApp.Documents.Add(strPath & conTemplate)
  28.  
  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
  42.  
  43.     ' Display the mail merge document.
  44.     wrdApp.Visible = True
  45.  
  46.  
  47. ExitHere:
  48.     Set doc = Nothing
  49.     Set wrdApp = Nothing
  50.     Set doc2 = Nothing
  51.     Set wrdApp2 = Nothing
  52.     Exit Sub
  53.  
  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
  62.  
  63.         End If
  64.  
  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.