Automating Mail Merge

E

Ed Shanley

I've got a database that has about 60 queries based upon various fields that
the user selects on a form. I would like to run a mail merge based upon
these fields and a text field where the user enters the path of the Word
document. I currently have the path defined in the field "txtMergeFile" and
have the proper query defined in "MergeQuery". Can anyone help me in
figuring out how to do this?

Thanks.
 
J

JohnR

Dim wrdApp As Object
Dim P1 As Object
Dim PM As Object
On Error Resume Next 'Get WOrd Object
Set wrdApp = GetObject(, "Word.Application")
If err Then
Set wrdApp = CreateObject("Word.Application")
End If
wrdApp.Activate
wrdApp.Visible = False
Set PM = wrdApp.Documents.Open(txtMergeFile)
With PM.MailMerge
.OpenDataSource name:=dbMerge, LinkToSource:=True,
Connection:="QUERY MergeQuery", _
SQLStatement:="Select * from [MergeQuery]"
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Set P1 = wrdApp.ActiveDocument 'New Merged Document
PM.Close False 'Close Merge Template
P1.SaveAs FileName:="Path and Filename" 'Save New Document
p1.close
Set wrdApp = Nothing
Set P1 = Nothing
Set PM = Nothing
 
T

Tom

Ed

Take a look at:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

Tom
JohnR said:
Dim wrdApp As Object
Dim P1 As Object
Dim PM As Object
On Error Resume Next 'Get WOrd Object
Set wrdApp = GetObject(, "Word.Application")
If err Then
Set wrdApp = CreateObject("Word.Application")
End If
wrdApp.Activate
wrdApp.Visible = False
Set PM = wrdApp.Documents.Open(txtMergeFile)
With PM.MailMerge
.OpenDataSource name:=dbMerge, LinkToSource:=True,
Connection:="QUERY MergeQuery", _
SQLStatement:="Select * from [MergeQuery]"
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Set P1 = wrdApp.ActiveDocument 'New Merged Document
PM.Close False 'Close Merge Template
P1.SaveAs FileName:="Path and Filename" 'Save New Document
p1.close
Set wrdApp = Nothing
Set P1 = Nothing
Set PM = Nothing

Ed Shanley said:
I've got a database that has about 60 queries based upon various fields
that
the user selects on a form. I would like to run a mail merge based upon
these fields and a text field where the user enters the path of the Word
document. I currently have the path defined in the field "txtMergeFile"
and
have the proper query defined in "MergeQuery". Can anyone help me in
figuring out how to do this?

Thanks.
 
Top