473,837 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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_Cl ick()

On Error GoTo MergeButton_Err

'Start Microsoft Word
Dim objWord As Word.Applicatio n
Set objWord = CreateObject("W ord.Application ")

With objWord
'Make the application visible.
.Visible = True

'Open the document.
.Documents.Open ("H:\privacymer ge.dot")

'Move to each bookmark and insert text from the form.
.ActiveDocument .Bookmarks("Tit le").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![Title]))
.ActiveDocument .Bookmarks("Fir stName").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![FirstName]))
.ActiveDocument .Bookmarks("Las tName").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![LastName]))
.ActiveDocument .Bookmarks("Add ress1").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![Address1]))
.ActiveDocument .Bookmarks("Add ress2").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![Address2]))
.ActiveDocument .Bookmarks("Sta te").Select
.Selection.Text = (CStr(Forms![frmPrivacy]![State]))
.ActiveDocument .Bookmarks("Pos tCode").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.ActiveD ocument.PrintOu t
Do While objWord.Backgro undPrintingStat us > 0

'Close the document without saving changes.
objWord.ActiveD ocument.Close SaveChanges:=wd DoNotSaveChange s

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

Exit Sub

MergeButton_Err :
'If a field on the form is empty, remove the bookmark text, and
If Err.Number = 94 Then
objWord.Selecti on.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 11510
13 New Member

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

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

Similar topics

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 each person for whom a mail merge letter is desired. The query reads address info from the table for each record where is true.
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 have a client record open in a form and click a button and for that particular record to merge with a preformatted word doc. I have created a query that gives me all the fields I need (as they are over multiple tables) and I can successfully combine...
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 document in Word; 2. opens the document and runs the merge process for the new data. I have managed to write the code to perform step 1 ok, but I'm having trouble with step 2. It opens the word document fine but does not perform the mail merge of...
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 associated it with a specific query in my Access database. My problem is that when I run my "create letters" button from an Access form, the program opens up Word correctly with the proper letter. However, the MailMerge toolbar in Word appears...
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 the same label multiple times on the printer. I have to print multiple labels on to the same page and then the printer. So, I think I have to use Mail-Merge and then Word Label Print. I also need the standard label printing dialog to open before...
by: lesperancer | last post by:
I have 3 tables (office97) tblQuote quoteNbr tblDetails ( quote : 1 <-> M: quoteDetails) quoteNbr detailLine product value
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 in the same table. If I use a control source that is selecting the 3 records, all of the data is shown but spread over 3 pages in the mail merge, but needs to be listed together in one paragraph. Is there anyway of looping through the 3...
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 for any comments about the use of the docmd.transfertext method instead of the code Albert used for creating the text file. Also, perhaps some ideas for coding the Subject Line of each email so
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 very little (actually probably nothing) about VBA and I'm currently learning my way around Access. Basically, I want to click a button in the form that merges the letter for me, only for the one record I'm currently viewing..not the whole...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.