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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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)
  End If
    Set oItem = Nothing

  SendEmail = True
  On Error Resume Next
  Set olApp = Nothing
  Set oItem = Nothing
  Exit Function
  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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

poster disappeared - solution works
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.