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.Applicatio n
Dim blnCreated As Boolean
Dim strFilename As String
Dim strQueryName As String
Dim strDBpath As String
strFilename = "e:\SUMMONSFORM .DOC"
strQueryName = "qryOWNERCODEFE NDANTsummonsmer gedata"
strDBpath = "E:\CityTaxSale V63.mdb" 'Or probably just CurrentDb.Name
On Error Resume Next
Set objWord = GetObject(, "Word.Applicati on")
If Err Then
Set objWord = CreateObject("W ord.Application ")
blnCreated = True
End If
On Error GoTo ErrHandling
Set objDoc = objWord.Documen ts.Open(strFile name)
'Make Word Visible
objWord.Visible = True
'Execute the MailMerge
With objDoc.MailMerg e
'Set Merge Data Source
objDoc.MailMerg e.OpenDataSourc e Name:=strDBpath , _
LinktoSource:=T rue, _
Connection:="QU ERY " & strQueryName, _
SQLStatement:=" SELECT * FROM " & strQueryName
.Destination = wdSendToNewDocu ment
.Execute
objWord.ActiveD ocument.PrintOu t False
objWord.ActiveD ocument.Close wdDoNotSaveChan ges
End With
'Close The form files and the merged document
objDoc.Close wdDoNotSaveChan ges
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*********@co mcast.net> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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_Cl ick()
Dim objWord As Word.Document
Set objWord = GetObject("e:\S UMMONSFORM.DOC" , "Word.Document" )
'Make Word Visible
objWord.Applica tion.Visible = True
'Set Merge Data Source
objWord.MailMer ge.OpenDataSour ce Name:="E:\CityT axSaleV63.mdb", _
LinktoSource:=T rue, _
Connection:="QU ERY qryOWNERCODEFEN DANTsummonsmerg edata", _
SQLStatement:=" SELECT * FROM qryOWNERCODEFEN DANTsummonsmerg edata"
'Execute the MailMerge
objWord.MailMer ge.Destination = wdSendToNewDocu ment
objWord.MailMer ge.Execute
objWord.Applica tion.Options.Pr intBackground = False
objWord.Applica tion.ActiveDocu ment.PrintOut
'Close The form files and the merged document
objWord.Close Word.wdDoNotSav eChanges
Set objWord = Nothing
End Sub