Updated: June 2020; Applicable to Microsoft Outlook 365 / 2019 / 2016
Here’s a note we got from a reader.
Thanks for your very useful Outlook coding help. I am wondering if you can share some code to help automate sending an email in Outlook? I know that there are several ways to send recurring message but i am specifically interested in using VBA for my this task.
Thanks for the question. Automatically sending emails seem to be among the most useful and popular tasks you can automate using VBA. Read on for more details and a simple script to get you started here.
Writing VBA for sending Outlook emails
In the rather simple example below, we’ll cover the following steps:
- First off, we’ll use the VBA Outlook object model to create a new email item.
- Then we’ll go ahead and set the message importance, recipients (separated by semi colons) and body text style and content.
- Then we’ll display the message in your Microsoft Outlook application.
- Last, we’ll go ahead and send it to the recipients.
Here’s the simple VBA code to automatically create and send simple emails. Note that you are to easily improve this script to include file attachments, tasks etc’, but let’s start with a very basic script.
Sub SendMail() Dim MyEmail As MailItem Set MyEmail = Application.CreateItem(olMailItem) With MyEmail .To = "<type your recipient email address/ess here>" .Importance = olImportanceHigh .Subject = "<type the subject of your email here>" .Body = "<type the email message text here>" .BodyFormat = olFormatHTML .Display End With 'I purposely commented the next line, uncomment it by removing the "'" sign to send your email. ' MyEmail.Send End Sub
Outlook VBA macro shortcut
You might want to look into associating the VBA code you just wrote with a command button in Outlook. This will allow you to easily launch it from a shortcut placed in the Outlook Ribbon.
Here’s how to do that:
- First, go ahead and open Microsoft Outlook.
- Hit File then select Options
- Now go ahead and select Customize Ribbon.
- In the Customize Ribbon section, you’ll notice the Choose Commands from list box.
- Select Macros
- Then Select your newly created VBA code, in our case the name should be SendMail.
- Then hit Add and then OK. Optionally you might want to assign an icon to your Macro.
- Your Macro will be available in the Quick Access toolbar in the upper left side of your Outlook application.
Possible issues with VBA macro settings
Your Outlook macro settings might need to be tuned in order to run macros. If so, here’s how to do that.