I have the following code which opens word successfully and merges the data but only after I get prompted to select a table. Is there a way around this. I am merging into Word 2003 from Access 2003.
Code:
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 = "D:\test\Master Due Diligence.docx"
strQueryName = "qryDDMergeReport"
strDBpath = "D:\test\DD.mdb"
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 Mail Merge
With objDoc.MailMerge
' Set the mail merge data source.
objDoc.MailMerge.OpenDataSource Name:=strDBpath, LinkToSource:=True, Connection:="QUERY" & strQueryName, SQLStatement:="SELECT * FROM" & strQueryName
.Destination = wdSendToNewDocument
.Execute
End With
Exit Function
ErrHandling:
MsgBox Err.Description
End Function