473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
1 11420
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

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

Similar topics

8
by: Squirrel | last post by:
Hi everyone, I've created a mail merge Word doc. (using Office XP) , the data source is an Access query. Functionality I'm attempting to set up is: User sets a boolean field to true for...
8
by: dd_bdlm | last post by:
Hi I wonder if anyone can help? I have looked through prevous posts and cant find any answers that fit what I need to do. For reference I am using Access 97 and Word 97. I want to be able to...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
4
by: pmhaupt2 | last post by:
I developed an Access 2003 program that will allow the user to produce a group of Word letters that merge with data records from an Access database. I created a mail merge Word document and...
1
by: achintya.jha | last post by:
Hi, I am currently working hard to print Word labels from ASP.NET I have multiple records which I need to print on to a word label print functionality. I know that Word Label printing prints...
4
by: lesperancer | last post by:
I have 3 tables (office97) tblQuote quoteNbr tblDetails ( quote : 1 <-> M: quoteDetails) quoteNbr detailLine product value
6
by: crealesmith | last post by:
Firstly, I have no problem with mail merging to Word, VB code for that works perfectly. On one mail merge I need to merge 15 fields of data that are from 3 seperate records. The 3 records are all...
7
by: giladp1 | last post by:
I found Albert Kallal's great "Super easy Word Merge" code in his site at: http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html Thanks Albert so much for sharing this. I am looking...
1
by: kayberrie | last post by:
I want to write a VBA mail merge code. I want to link the code/macro/dohicky to a nifty little button so it makes life easy. I think I can handle the button part, the code part - not so much. I know...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.