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