I have a database of training courses and I want to be able to email all enrolled participants invitations. I have a query that shows all participants enrolled in a specific session. The query works fine when run from a form in Access.
Here is the code I'm using for the mail merge:
Code:
Dim oWord As Word.Application Dim oWdoc As Word.Document
Dim wdInputName As String
Dim wdOutputName As String
Dim outFileName As String
' Set Template Path
'------------------------------------------------
wdInputName = CurrentProject.Path & "\MailMergeDocs\CatIII.docx"
' Create unique save filename with minutes
' and seconds to prevent overwrite
'------------------------------------------------
outFileName = "MailMergeFile_" & Format(Now(), "yyyymmddmms")
' Output File Path w/outFileName
'------------------------------------------------
wdOutputName = CurrentProject.Path & "\completed\" & outFileName
Set oWord = New Word.Application
Set oWdoc = oWord.Documents.Open(wdInputName)
' Start mail merge
'------------------------------------------------
With oWdoc.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:=CurrentProject.FullName, _
AddToRecentFiles:=False, _
LinkToSource:=True, _
Connection:="QUERY qryFirstAidMailMerge", _
SQLStatement:="SELECT * FROM [qryFirstAidMailMerge]"
.Destination = wdSendToEmail
.MailAsAttachment = False
.MailFormat = wdMailFormatHTML
.Execute Pause:=False
End With
' Hide Word During Merge
'------------------------------------------------
oWord.Visible = False
' Quit Word to Save Memory
'------------------------------------------------
oWord.Quit savechanges:=False
' Clean up memory
'------------------------------------------------
Set oWord = Nothing
Set oWdoc = Nothing