MarkusArchus
Programmer
I am using the following code to open a Word document and use an Access query as the data source. (For test purposes I've hard coded the file and query names but eventually they will be variables.)
Function testmerge()
Dim db As Database, wrd As Object
Set db = CurrentDb
Set wrd = GetObject("d:\apprentice\dc35\merge test.doc", _
"Word.Document"
wrd.Application.Visible = True
wrd.mailmerge.OpenDataSource _
Name:=db.Name, _
LinkToSource:=True, _
Connection:="QUERY Apprentice Merge", _
SQLStatement:="select * from [Apprentice Merge]"
wrd.mailmerge.viewmailmergefieldcodes = False
Set wrd = Nothing
End Function
This works fine with a plain vanilla query. But if I add a prompt to the query and run this code, I get prompted as I normally would when running the query, but then after I enter data at the prompt, I get an error message: "Word was unable to open the data source."
I use prompt queries all the time in mail merges that I set up manually, whether I use the mail merge wizard in Word, or the Office Links in Access. Is there something special I need to do to make it work when I use VBA code to run the merge?
TIA.
Function testmerge()
Dim db As Database, wrd As Object
Set db = CurrentDb
Set wrd = GetObject("d:\apprentice\dc35\merge test.doc", _
"Word.Document"
wrd.Application.Visible = True
wrd.mailmerge.OpenDataSource _
Name:=db.Name, _
LinkToSource:=True, _
Connection:="QUERY Apprentice Merge", _
SQLStatement:="select * from [Apprentice Merge]"
wrd.mailmerge.viewmailmergefieldcodes = False
Set wrd = Nothing
End Function
This works fine with a plain vanilla query. But if I add a prompt to the query and run this code, I get prompted as I normally would when running the query, but then after I enter data at the prompt, I get an error message: "Word was unable to open the data source."
I use prompt queries all the time in mail merges that I set up manually, whether I use the mail merge wizard in Word, or the Office Links in Access. Is there something special I need to do to make it work when I use VBA code to run the merge?
TIA.