Mail merge with multiple rows of data

O

omsoft

I need to do mail merge to each of the customers with their open invoices. I
have the customer name, address, etc. code working. Now I need to add open
invoices. At the outset, I do not know how many open invoices each customer
has. How can I manage the number of rows in merged document and template
using VBA?

Dim strDir As String
Dim objWord As New Word.Application
Dim objDoc As Word.Document
strDir = getMDBPath
objWord.Application.Visible = True
Set objDoc = objWord.Documents.Open(strDocName)
objWord.Application.Visible = True

objDoc.MailMerge.OpenDataSource _
Name:=getThisDBPath(), _
LinkToSource:=True, AddToRecentFiles:=False, _
Connection:="QUERY qry_CusMail", _
SQLStatement:="SELECT * FROM [qry_CusMail]"
objDoc.MailMerge.Destination = wdSendToNewDocument
objDoc.MailMerge.Execute

objWord.Application.Documents(1).SaveAs (strMergedDocName & ".doc")
objWord.Application.Documents(2).Close wdDoNotSaveChanges
objWord.Application.Documents(1).Close wdSaveChanges
objWord.Quit
Set objWord = Nothing
Set objDoc = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top