Use an Access 2016 query to send mail merged emails via Outlook 2016

Hi Experts,

How can I use an Access 2016 query to send mail merged emails via Outlook 2016?

Regards,
Leigh
LVL 1
LeighWardleAsked:
Who is Participating?
 
COACHMAN99Connect With a Mentor Commented:
you would write a vba function that leverages the outlook libraries, and call it in a loop, each time passing the unique address/subject data

prior to calling the above function, you would use another function to embed your merge data in the body text, then pass it to the above function as an argument

you would also need to decide whether you are sending HTML or plain text as the 'body' argument would need to be populated accordingly

e.g.
Function SendEmail(RecipEmail As String, Subject As String, BBody As String, cc1 As String) As Boolean
  On Error GoTo err_SendEmail
  Dim olApp As Outlook.Application
  Dim oItem As Outlook.MailItem
  Dim tInterval As Integer, signature As String
  If RecipEmail <> "" Then
    SendEmail = False
    Set olApp = CreateObject("Outlook.Application")
    Set oItem = olApp.CreateItem(olMailItem)
    With oItem
      .To = RecipEmail & IIf(cc1 <> "", "; " & cc1, "")
      .Display   'to get signature for later use
      signature = Nz(DLookup("CurricCoordSignature", "tblPrograms_LOOKUP", "Prog_Num=" & Nz(Forms("frmMain").cboProgram, 0)), 0) '.Body
      .Subject = Subject
      .Body = BBody & vbCr & vbCr & signature
    End With
 
    If Nz(gAttach1, "") <> "" Then oItem.Attachments.Add (cAttachmentPath & gAttach1)
    If Nz(gAttach2, "") <> "" Then oItem.Attachments.Add (cAttachmentPath & gAttach2)
    If Nz(gAttach3, "") <> "" Then oItem.Attachments.Add (cAttachmentPath & gAttach3)
    If Nz(gAttach4, "") <> "" Then oItem.Attachments.Add (cAttachmentPath & gAttach4)
    If Nz(gAttach5, "") <> "" Then oItem.Attachments.Add (cAttachmentPath & gAttach5)
    DoEvents
    oItem.Display
  End If
NextItem:
    Set oItem = Nothing
    DoEvents
exitPoint:

  SendEmail = True
exit_SendEmail:
  On Error Resume Next
  Set olApp = Nothing
  Set oItem = Nothing
  Exit Function
err_SendEmail:
  MsgBox "Error " & Err.Number & ", " & Err.Description & " Occurred in SendEmail after " & ErrorLine
  WriteErrorLog "Error " & Err.Number & ", " & Err.Description & " Occurred in SendEmail after " & ErrorLine
  Resume exit_SendEmail
End Function
0
 
GrahamSkanRetiredCommented:
Word's Mail Merge can use an Access  query as a datasource. There are some restrictions. It can't be one-to-many or use parameters.

If the Sql doesn't have to change between runs, then you can set up a merge that will run without coding. It will use your default mail application (Outlook if that is what you use).  You can set it up and run it from Access via the Word Merge button on the External Data tab

I
0
 
COACHMAN99Commented:
poster disappeared - solution works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.