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

Last updated: October 2019

Applicable to: Office 365 subscriptions. Office 2019,  2016, 2013, 2010 ; Windows 10, 8, 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 to these VBA program as Macros.

In this tutorial, we will focus on learning the basics of Outlook VBA for beginners:

  • We’ll start by quickly setting up the macro development environment in our computer.
  • We’ll go through some simple examples of VBA scripts for Outlook. This will help you to grasp the Macro development basics.
  • We’ll learn how to insert a custom VBA macro into an 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 settings to ensure that the code will actually run when prompted in our computers.

Can i record an Outlook Macro?

Unlike other Office apps, Outlook does not offer a simple Macro recorder to capture user actions and auto-generate 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.

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 - - 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.


Useful Outlook VBA macro 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 = ""
.Subject = "Insert your message subject here"
.Body = "Insert your email text body here"
.CC = ""
End With
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.


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 = ";"
.Subject = "This is your message subject"
.Body = "Insert your email text body here"
.CC = ""
.Attachments.Add AttachFolder & AttachFile
End With
End Sub


  • 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
.Recipients.Add ""
.Subject = "This is your task subject"
.Body = "Insert a thorough explanation of your task here."
End With
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")
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?

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, go ahead and hit Alt +F11.
  3. The VBA developer Integrated Development Environment (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?

You might be wandering where eactly your used Macro is stored. Well, 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. Specifically, the file storing all you Macros is named VBAProject.otm. The file can be found at: C:\users\<your_user_name>\AppData\Roaming\Microsoft\Outlook.

Shortcut button for Outlook macros

Our next step is to insert a small shortcut 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 - - 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 - - Outlook

  1. Select your Macro security settings.
    1. In Outlook 2016: Select Notifications for only digitally signed macros, all other disabled or Notifications for all macros .
    2. In Outlook 2019: Select Disable All Macros with Notification. This will post a warning message requiring user approval before your macro runs.
  1. After setting your Macro security settings, hit OK.

How to use 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 and run your (first) Outlook VBA macro!

Additional Outlook Macros ideas

Several of our readers asked whether we could specify further ideas for Outlook Automation. Here are a few ones to get started.

  1. Automatically save attachments in incoming messages into a specific folder.
  2. Read a list of appointments entries from a CSV file and automatically create appointments in one or more calendars.
  3. Automatic task/meeting/contact creation out of incoming emails.
  4. Visualize status of attendees response status to a set of recurring meetings.
  5. Send Outlook emails from Excel, Word or Access.
  6. Download all attachments from a specific Outlook folder into your computer. If your Outlook Account space is limited, you could use this macro to specifically download only large attachments (over 5Mb for instance) to your disk, insted of managing them in Outlook.
  7. Export Outlook contacts directly into an Access database. Note that Export contacts to CSV is available in the product.
  8. Automatically update meeting information for individual or recurring appointments.

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:

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.

Lasantha says June 20, 2016

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

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?

Raharinesy says July 23, 2019

Could you send us an exemple of macro in outlook saving joined file in e specific directory like c:\mydir.
Thanks in advance for your help

Peder says August 12, 2019

Thanks for a excellent explanation. Is there a way to assign a keyboard shortcut? Tried to search around but it looks like its no solution?

Add Your Reply