I have to say that I have been super hesitant to share this example as it could be used by spammers. That being said, there are plenty of legit uses for sending email through Outlook with VBA.
I have used this function primarily to automate the delivery of scheduled reports where I pull in an HTML template (fancy), replace template “placeholders” and have the report sent to a distribution list defined in an access database table. I won’t go into details about how to get ‘fancy’ but rather let you as an end-user modify to your needs.
Provided are two examples, one with early binding and another with late binding code. Starting off, here is a test function you can reference for calling the sendmail function examples.
Send Email with VBA through Outlook – Test Function
'----------------------------------------------------
' Test Function. Change the 'mail@example.com' string
' to your Email address
'----------------------------------------------------
Sub test()
Call sendEmail("mail@example.com", "This is a test", "This is a test")
End Sub
Send Email Through Outlook with VBA – Early Binding Example
'----------------------------------------------------
' Send Email through Outlook with VBA (Early Binding)
'----------------------------------------------------
' NOTE: To use this code, you must reference
' The Microsoft Outlook 14.0 (or current version)
' Object Library by clicking menu Tools > References
' Check the box for:
' Microsoft Outlook 14.0 Object Library in Outlook 2010
' Microsoft Outlook 15.0 Object Library in Outlook 2013
' Click OK
'----------------------------------------------------
Sub sendEmail(msgTo, msgSubject, msgBody)
Dim oApp As Outlook.Application
Dim oMail As MailItem
' Initialize Outlook and Set Objects
'------------------------------------------
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
'Set Options
With oMail
'Message format set
'HTML, Plain, RichText or Unspecified
'--------------------------------------
.BodyFormat = olFormatHTML
'Set Recipient(s)
'--------------------------------------
.To = msgTo
'Set Subject Line
'--------------------------------------
.Subject = msgSubject
'Body Text
'--------------------------------------
.HTMLBody = msgBody
End With
'Send the message
'------------------------------------------
oMail.Send
' Quit Outlook Application After Send
'------------------------------------------
' If you want to leave outlook running
' in the background after sending your
' message, leave 'oApp.Quit' commented out
'------------------------------------------
'oApp.Quit
' Clean Memory
'------------------------------------------
Set oApp = Nothing
Set oMail = Nothing
End Sub
Send Email Through Outlook with VBA – Late Binding Example
'----------------------------------------------------
' Send Email through Outlook with VBA (Late Binding)
'----------------------------------------------------
' NOTE: This is the late binding version of the
' Send Email through Outlook with VBA code. No ref
' to Microsoft Outlook XX.0 Object Library is needed
'----------------------------------------------------
Sub sendEmail(msgTo, msgSubject, msgBody)
Dim oApp As Object
Dim oMail As Object
' Initialize Outlook and Set Objects
'------------------------------------------
Set oApp = CreateObject("Outlook.Application")
' (0) = olMailItem for late binding
'------------------------------------------
Set oMail = oApp.CreateItem(0)
'Set Options
With oMail
'Message format set (enum)
'HTML(2), Plain(1), RichText(3) or Unspecified(0)
'--------------------------------------
.BodyFormat = 2
'Set Recipient(s)
'--------------------------------------
.To = msgTo
'Set Subject Line
'--------------------------------------
.Subject = msgSubject
'Body Text
'--------------------------------------
.HTMLBody = msgBody
End With
'Send the message
'------------------------------------------
oMail.Send
' Quit Outlook Application After Send
'------------------------------------------
' If you want to leave outlook running
' in the background after sending your
' message, leave 'oApp.Quit' commented out
'------------------------------------------
'oApp.Quit
' Clean Memory
'------------------------------------------
Set oApp = Nothing
Set oMail = Nothing
End Sub
As always please comment with questions, improvements, etc…
Ron says
Thank you for the post. I support nearly 50 Access databases and was looking for a “Late Binding” option. Since the office is currently transitioning from Office 2007 to 2013, I need to replace a lot of “Early Binding” code in order for these different formats to co-exist on our network. This looks like just what I needed.