• Skip to main content
  • Skip to primary sidebar

Ryan McCormick

Send Email Through Outlook with VBA

November 23, 2015 by Ryan 1 Comment

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…

Filed Under: Microsoft Access, Microsoft Excel, Microsoft Outlook, VBA Tagged With: outlook, send email, vba

Reader Interactions

Comments

  1. Ron says

    August 23, 2016 at 6:15 am

    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.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Force Quit Kill all Chrome Windows MacOS
  • SOLVED: Angular 6 CLI Karma Stuck in Single Run | Karma Stops Running
  • How to Manually Install Java 8 on Ubuntu 18.04 LTS
  • Remove VirtualBox from Ubuntu 16.04 Xenial
  • Clear all Node Modules Folders Recursively Mac/Linux

Recent Comments

  1. KKV on Webstorm adding spaces between imports and braces | JavaScript and TypeScript
  2. jusopi on Clear all Node Modules Folders Recursively Mac/Linux
  3. Qaisar Irfan on Clear all Node Modules Folders Recursively Mac/Linux
  4. mustafa on Remove VirtualBox from Ubuntu 16.04 Xenial
  5. Pourya on How to Manually Install Java 8 on Ubuntu 18.04 LTS

Copyright © 2025 · Magazine Pro on Genesis Framework · WordPress · Log in