How to create Outlook macros to automate email tasks?

“I need to process dozens of similar emails each day, extracting information and filing them in specific folders. It’s taking up hours of my time. Is there a way to automate this in Outlook without purchasing third-party software? I’ve heard about macros but don’t know where to start or if they’re difficult to set up.”

Why automating Outlook tasks with VBA scripts matters

Email overload is a common productivity killer in today’s workplace. Manual processing of repetitive email tasks not only consumes valuable time but also increases the risk of human error. Microsoft Outlook offers a powerful solution through its built-in macro functionality using Visual Basic for Applications (VBA). By creating custom automation in Outlook, you can transform hours of manual work into seconds of automated processing. We’ll use Outlook’s VBA editor to develop a solution that works seamlessly with the desktop application.

Step-by-step guide to write VBA scripts for Outlook

In this tutorial, we wanted to provide the web’s most complete Macro development resource for Outlook. We will focus on exploring the basics of Outlook VBA for beginners, learn to write macros and then look into some more advanced useful macro ideas. Based on feedback we got, after going though this tutorial, you should be able to develop and run simple VBA macros for Outlook.

  • Basics first: We’ll learn how to insert and save a simple custom VBA macro into an active Outlook Session.
  • We’ll then set up the macro development environment in our computer.
  • We’ll go through the most basic examples of Outlook VBA. This will help you to grasp the Macro development basics and allow you to write simple macros.
  • 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.

Writing VBA Macros in Outlook

  1. First off, open Microsoft Outlook.
  2. Now, go ahead and hit the Alt button + 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. To accomplish that, go ahead and select Module.
  7. Paste your VBA code into the Module window.
  8. Hit File then select Save.
  9. Go ahead and close the Developer environment. This will bring you back to your Outlook user interface.

Note: Read on for some simple code examples to get you started with VBA. Never copy VBA code from unknown resources.

How to setup the developer tab in Outlook?

If you’ll be frequently developing Outlook Macros, you may want to set up your Office development environment. Sounds fancy isn’t it? Actually it’s quite a simple procedure. Read on for the details:

  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.

Useful Outlook VBA examples

Create an email message

This simple snippet creates a new email message item programmatically.

The script starts by creating an Outlook mail item, 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 manually.

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 Outlook template file (*. oft ).

 

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 of the previous snippet.

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 spreadsheet, Powerpoint presentation, PDF documents etc’. If you want to send an email with attachments programmatically, use this code:

#VBA Code
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, kindly adjust as required.
 

Create a task with VBA

The following snippet creates an Outlook task, assigns it to another individual 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
 

Note: Ensure that you launch this code only on emails that are open in Outlook (not in the Inbox/Explorer view).

Outlook Macros related questions

Where are Macros stored in Outlook?

You might be wandering where exactly 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.