Adapting some code I got here last week for another database (in which it is working well) I am having a problem opening an Access database to do a mailmerge in Word. The database that can't be open is the one that is calling the mail merge. Here is the code with the porblem area in red:
Any suggestions?
Bob
Code:
Public Function MergeIt()
'------------------------------------------------------
'This code opens a Word template from Access, merges the
'data and prints the resultant documents
'------------------------------------------------------
Dim odoc As Word.Document, objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set odoc = objWord.Documents.Add("C:\Program Files\Microsoft Office\Templates\AccessSurgRpt.dot")
[COLOR=red] odoc.MailMerge.OpenDataSource _
Name:="C:\My Documents\Surgery & Analgesia Database.mdb", _
linktosource:=True, _
Connection:="QUERY qrySurgRpt", _
SQLStatement:="Select * FROM qrySurgRpt"[/color]
odoc.MailMerge.Execute
odoc.PrintOut
End Function
Any suggestions?
Bob