Option Compare Database Option ExplicitFunction MergeIt(stFilePath As String, stQuery As String) Paramaters: stFilePath As String - Full path to merge letter template stQuery As String - SQL String for merge fields data from the CurrentDB Dim stDBName As String Dim objWord As Word.Document Dim stSQL As String stSQL = "Select * from " & stQuery & "" stQuery = "QUERY " & stQuery stDBName = CurrentDb.Name Set objWord = GetObject(stFilePath, "Word.Document") ' Make Word visible. objWord.Application.Visible = True ' Set the mail merge data source as the Northwind database objWord.MailMerge.OpenDataSource _ Name:=stDBName, _ LinkToSource:=True, _ Connection:=stQuery, _ SQLStatement:=stSQL ' Execute the mail merge. objWord.MailMerge.Execute End Function |