I have the below code for a mail merge. It works just fine (in my previous post I could not get it to work....I had mis-spelled the document name). From my limited understanding of this code it assigns the "data source" to the mail merge document. Therefore, if my logic is....logical, I don't need the "SELECT" statement within the document itself. However, if I don't have the "SELECT" statement in the document I cannot tell Word where to put the merged fields. The reason I would like to assign the "data source" in VBA is because I believe it will not show the "SELECT" warning message that you get when opening a Mail Merge document. This is most definitely desired because I intend on passing this off to an end-user. Everything I have read states you must make some registry changes to "suppress" the message...Not an option for me. I guess my question is this...Can you insert the merge fields using VBA, and tell Word where to put them on the page?
Code:
Function funMergeItDM()
Dim strPathFile As String, strTable As String
Dim objDesktop As Object
Dim ObjWord As Word.Document
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Set objDesktop = CreateObject("WScript.Shell")
strPathFile = objDesktop.SpecialFolders("Desktop") & "\DMContactListEnvelope.docx"
Set ObjWord = GetObject(strPathFile, "Word.Document")
With ObjWord
.Application.Visible = True
.MailMerge.OpenDataSource _
Name:="C:\Data\DMPhoneList_Be.accdb", _
LinkToSource:=True, Connection:="QUERY qryDMMailMerge", _
SQLStatement:="SELECT * FROM [qryDMMailMerge]"
.MailMerge.Execute
End With
End Function