How to insert custom VBA macros into Outlook 2016 and 2019 e-mail messages?


Last updated: June 2019

Applicable to: Office 365,  2016, 2013, 2010, 2019 ; Windows 10, Windows 7 operating systems.

In Microsoft Office, we use Visual Basic for Applications (VBA) for creating custom programs that enhance Office’s basic functionality and saving user time by automating repetitive tasks. We refer tho these VBA program as Macros.

Unlike other Office apps, Outlook doesn’t offer a simple Macro recorder that allows us to record user actions and auto-generates the required VBA code for us in the background. Therefore, if we want to add our own custom functionality to Outlook, we’ll need to learn to write simple VBA programs and incorporate them into our Outlook session.

Note: If you are interested to know how the Office macro recorder actually works, i would recommend to look into the MS Word Macros and Excel VBA Macro tutorials.

In this tutorial, we will focus on learning the basics related to using VBA in Outlook:

  • We’ll start by setting up your macro development environment.
  • We’ll go through some of the most useful VBA scripts for Outlook.
  • We’ll learn how to insert our custom VBA macro into the active Outlook Session.
  • Next, we’ll go ahead and define a shortcut icon for our macro in the Quick Access Toolbar. This will allow the us to run the macro with ease.
  • And we’ll finish by enabling Outlook macros and handling their security to ensure that the code will actually run when prompted in our computers.

How to setup the developer tab in Outlook?

Before you start writing your code, you may need to set up your Office development environment. Sounds fancy isn’t it? Actually it’s quite a simple procedure:



  1. Open Outlook, right click on the upper Ribbon area and select Customize the Ribbon.

2015-08-23 20_43_49-Inbox - vinod06061996@gmail.com - Outlook

  1. Check the Developer entry and hit OK.

2015-08-23 20_44_44-Outlook Options

  1. Now you will find the Developer menu in the Ribbon.

 

Most useful Outlook VBA examples

Create an email message

This simple snippet creates a new email message item programmatically; then it sets the to, subject, body text and CC fields. Finally it displays the message in Outlook so you can review and send it.

Public Sub CreateMail()
Dim MyEmail As MailItem
' Create a new Outlook message item programatically
Set MyEmail = Application.CreateItem(olMailItem)
'Set your new message to, subject, body text and cc fields.
With MyEmail
.To = "recipient@myserver.com"
.Subject = "Insert your message subject here"
.Body = "Insert your email text body here"
.CC = "cc@myserver.com"
End With
MyEmail.Display
End Sub

Advanced Note: You are able to use predefined Outlook templates when defining new emails with VBA. In order to do that, you’ll need to leverage the method CreateItemFromTemplate and specify the location of your oft file.

Send an email programmatically

If you want Outlook to send out the email you have just created automatically, you can add the following line to the code posted above. Paste the following line before the End Sub statement.

MyEmail.Send
Attach a file to an email

In many instances you would like to automate sending an email with a file attached to it. This could be a Word document, Excel spreadseheet, Powerpoint presentation, PDF documents etc’. If you want to send an email with attachments programmatically, use this code:

Public Sub CreateMailWithAttachments()
Dim MyEmail As MailItem
' Create a new Outlook message item programatically
Set MyEmail = Application.CreateItem(olMailItem)
'Define your attachment folder path and file name - modify this part as needed
Dim AttachFolder, AttachFile As String
AttachFolder = "C:\"
AttachFile = "test.txt"
'Set your new message to, subject, body text and cc fields.
With MyEmail
.To = "recipient@my_server.com; mysecondrecipient@my_server.com"
.Subject = "This is your message subject"
.Body = "Insert your email text body here"
.CC = "cc@my_server.com"
.Attachments.Add AttachFolder & AttachFile
End With
MyEmail.Display
End Sub

Notes:

  • Please ensure that you set the correct path and attachment file name in the snippet.
  • In this example i have set multiple recipients to the email.
Create a task with VBA

The following snippet, creates a task and assigns it to another individua and sets the task subject and body text.

Public Sub CreateTask()
Dim MyTask As TaskItem
' Create a new Outlook task
Set MyTask = Application.CreateItem(olTaskItem)
'Set your new task recipients and information fields
With MyTask
.Assign
.Recipients.Add "taskrecipient@my_server.com"
.Subject = "This is your task subject"
.Body = "Insert a thorough explanation of your task here."
End With
MyTask.Display
End Sub
Save as HTML

Few readers asked how to automate saving emails to HTML. The code below accomplishes exactly that.

Note: Ensure that you launch this code only on open emails.

Public Sub SaveAsHTML()
On Error Resume Next
Dim MyWindow As Outlook.Inspector
Dim MyItem As MailItem
Dim FilePath As String
FilePath = Environ("HOMEPATH") & "\Documents\" & "\"
Dim ItemName As String
Set MyWindow = Application.ActiveInspector
If TypeName(MyWindow) = "Nothing" Then
MsgBox ("Kindly open an email to save")
Else
Set MyItem = MyWindow.CurrentItem
ItemName = MyItem.Subject ' File name will be identical to the message subject
With MyItem
.SaveAs FilePath & ItemName & ".html", olHTML
End With
End If
End Sub

How to use VBA Macros in Outlook?

For this example, we’ll use the following Visual Basic macro code which allows to create an e-mail message programmatically.

Adding the macro to Outlook

Our next step will be to insert one of the VBA code snippets displayed above, or other code, into your Outlook session. Here are the steps to follow:

  1. First off, Open Microsoft Outlook.
  2. Now, hit Alt +F11.
  3. The VBA developer IDE will be opened.
  4. Expand the project tree until the ThisOutlookSession node.
  5. Right click ThisOutlookSession and then hit Insert.
  6. You’ll now going to inset a Visual Basic module. Go on and select Module.
  7. Paste the code above into the Module window.
  8. Hit File then select Save.
  9. Go ahead and close the Developer tab. This will bring you back to your Outlook user interface.

Where are Macros stored in Outlook?

Unlike in Excel for example, which allows you to store macros in the personal.xlb file or in specific spreadsheets; all Microsoft Outlook macros are stored in one global file in your file system:  VBAProject.otm. The file can be found at: C:\users\<your_user_name>\AppData\Roaming\Microsoft\Outlook.

How to create an icon for your macro?

Our next step is to insert a small button which will allow us to easily run the macro. For simplicity, we’ll insert this button to the Quick Access toolbar.

  1. Open Microsoft Outlook, if it’s not opened yet.
  2. Right click on the upper Outlook Ribbon and select Customize the Quick Access Toolbar.

2015-08-23 20_56_38-Inbox - vinod06061996@gmail.com - Outlook

       Note: You can use a similar procedure to place your icon in the Ribbon itself instead of in the Quick Access Toolbar.

  1. Hit the  ‘Choose commands from‘ combo box and select Macros.

2015-08-23 20_57_52-Outlook Options

  1. Select the macro you just created and hit Add.

2015-08-23 21_00_05-Outlook Options

  1. Hit Modify to define a custom icon picture for your Quick access toolbar button.

2015-08-23 21_01_18-Outlook Options

  1. When done, hit OK.

How to enable a macro in Outlook?

This step is optional and should be followed only if you receive an error message when running your newly created macro.

  1. Next is to allow Outlook to run our VBA macro. By default, Microsoft Office disables macros and doesn’t allow them; so we’ll need to define the proper macro security to allow your custom Outlook macro to run.
  2. In Outlook 2016, click on Developer in the ribbon and select Macro security.

2015-08-23 21_03_18-Inbox - vinod06061996@gmail.com - Outlook

  1. Select your macro security settings:

            Tip: assign notifications for only digitally signed macros or set notifications for all macros .

  1. After setting your Macro Security settings, hit OK.

How to run your Outlook macro?

Last but definitely not least, now it’s time to run your newly created macro.

  1. Navigate to the Quick Access toolbar (upper left hand side of the screen – slightly above the HOME menu).
  2. Hit the icon you have assigned to your macro.
  3. Voi’la, you have just completed your (first) Outlook VBA macro!

Where to go next?

  • Need specific VBA help? Feel free to contact us.
  • Got a related question? Look into our guide to get your questions answered or feel free to leave us a comment.

Leave a Comment:

3 comments
Matt says October 16, 2015

Are you finding that the ‘Notifications for digitally signed macros’ and ‘Notifications for all macros’ settings are not working in Outlook 2016?

I only seem to be able to get my macro (which is self-signed) to run with ‘enable all macros’ on. #

Worked fine in Outlook 2013, can’t seem to get it to work in 2016.

Can’t figure out if bug in Outlook 2016 or not.

Reply
Lasantha says June 20, 2016

Thanks a lot. This will helps to save 400 min (~7 hrs) per year for me.

Reply
Michael Charry says April 10, 2019

If this isn’t the right spot to post please refer me where but how would I create a macro that uses an outlook signature so that I can create a button that will add the signature to a reply or forwarded email and also add a file attachment?

Reply
Add Your Reply