How about something like...
(Perhaps switching the three strings to passed arguments to make it a little
more versatile?)
Function MergeIt()
On Error GoTo ErrHandling
Dim objDoc As Word.Document
Dim objWord As Word.Application
Dim blnCreated As Boolean
Dim strFilename As String
Dim strQueryName As String
Dim strDBpath As String
strFilename = "e:\SUMMONSFORM.DOC"
strQueryName = "qryOWNERCODEFENDANTsummonsmergedata"
strDBpath = "E:\CityTaxSaleV63.mdb" 'Or probably just CurrentDb.Name
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err Then
Set objWord = CreateObject("Word.Application")
blnCreated = True
End If
On Error GoTo ErrHandling
Set objDoc = objWord.Documents.Open(strFilename)
'Make Word Visible
objWord.Visible = True
'Execute the MailMerge
With objDoc.MailMerge
'Set Merge Data Source
objDoc.MailMerge.OpenDataSource Name:=strDBpath, _
LinktoSource:=True, _
Connection:="QUERY " & strQueryName, _
SQLStatement:="SELECT * FROM " & strQueryName
.Destination = wdSendToNewDocument
.Execute
objWord.ActiveDocument.PrintOut False
objWord.ActiveDocument.Close wdDoNotSaveChanges
End With
'Close The form files and the merged document
objDoc.Close wdDoNotSaveChanges
If blnCreated Then
objWord.Quit
End If
Set objDoc = Nothing
Set objWord = Nothing
Exit Function
ErrHandling:
msgbox "Whoops" 'Better error handling of course
End Function
"Ray Todd Jr" <sc*********@comcast.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I'll post the entire code below. When I click on the button (that the
code is attached to), it opens up two instances...:
Any advice/suggestions you can provide would be helpful...
Ray.
Private Sub PrintSummons_Click()
Dim objWord As Word.Document
Set objWord = GetObject("e:\SUMMONSFORM.DOC", "Word.Document")
'Make Word Visible
objWord.Application.Visible = True
'Set Merge Data Source
objWord.MailMerge.OpenDataSource Name:="E:\CityTaxSaleV63.mdb", _
LinktoSource:=True, _
Connection:="QUERY qryOWNERCODEFENDANTsummonsmergedata", _
SQLStatement:="SELECT * FROM qryOWNERCODEFENDANTsummonsmergedata"
'Execute the MailMerge
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
'Close The form files and the merged document
objWord.Close Word.wdDoNotSaveChanges
Set objWord = Nothing
End Sub