• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
    • Common Problems
    • Outlook BCM
    • Utilities & Addins
    • Video Tutorials
    • EMO Archives
    • Outlook Updates
    • Outlook Apps
    • Forums

How to use Outlook's VBA Editor

Slipstick Systems

› Developer › How to use Outlook’s VBA Editor

Last reviewed on March 29, 2018     274 Comments

July 29, 2011 by Diane Poremsky 274 Comments

The steps are the same for all versions of Outlook and all Office applications

It's easy to find code samples on the Internet, but unless you know how to use them, they aren't going to do you much good.

We're going to assume the code is 100% ready-to-use, not a code snippet that is half complete.

If the code will be run against items in your mailbox or personal folders, you should make a backup of the mailbox or data file, just in case something goes wrong with the code. If the code runs against a specific folder, copying the folder and contents may be enough "insurance", (You should have routine backups regardless, but I know many users don't bother.) At least very least, make a copy of the items in the folder you are running the code against.

Check Macro Security Level

Before you begin, you'll need to check your macro security setting, otherwise, you'll need to use selfcert.exe to sign your macros to test them.

Enable Macros in Outlook's Trust Center

Outlook 2007 and older: Access the dialog to change the security level from Tools, Macros, Security.

Outlook 2010 and up: File, Options, Trust Center, Trust Center Settings, Macro Security.

It's highly recommended that you set the Macro Security level to only allow digitally signed macros. Do not choose the Low option (run all, never ask), except during testing. Note that some security software will set it to High and your macros will not run. You may need to restart Outlook after changing the security setting.

Open the VBA Editor

Now you are ready to open the VBA Editor. Press Alt+F11 on your keyboard, or if you are showing the Developer ribbon, click theVisual Basic Editor command to open it. In Outlook 2007 and older, the command in on the Tools, Macros menu.

Visual Basic Editor

If your screen does not resemble the screenshot above, with the white editing pane open, you'll need to click on Microsoft Outlook Objects to expand it then double click on ThisOutlookSession to open it in the editing pane on the right side.
To add a module to the VBA project, right click on Project1 and choose Insert > Module to insert a new module.

Generally speaking, you can put the code into either ThisOutlookSession or a Module, unless otherwise noted that it must be in ThisOutlookSession or a module.

Application_Start macros must be in ThisOutlookSession.

 

Starting out with Outlook Macros Video Tutorial

This video tutorial shows you have to change the macro security settings, add a macro to Outlook, run the macro, and then create a digital signature using SelfCert.exe to sign the macro before increasing macro security.

Run the Macro

Press F5 or the Run button to run the macro from the VBA editor. (It's highly recommended you make a backup of the folders or message store before running macros.)
Click the button to run the macro

Ctrl+Break will pause the macro, or press the Stop button on the toolbar to end it.

If you will be using the macro often, you should use selfcert to sign it then set Macro Security to only allow signed macros.

Don't sign the macro until you are done tweaking it and if you tweak it later, you'll need to re-sign it.

To run the macro from Outlook 2007 or older, go to Tools, Macro, Macros and select the macro. In Outlook 2010, you need to show the Developer ribbon first (File, Options, Customize Ribbon - check Developer ribbon on the right). You can also open the Macros dialog using Alt+F8 (all versions). You can also add macros to toolbar or ribbon buttons.

 

Using SelfCert to sign a macro

To sign a macro using selfcert, you first need to create a certificate. You can find selfcert in the folder with your Office files. In Outlook 2010 and older (as well as Outlook 2013 in volume licensing), selfcert is usually at C:\Program Files\Microsoft Office\OfficeXX where XX is your version of Office.

self-cert

In Outlook 2013 and 2016 "click to run", users will find selfcert at C:\Program Files\Microsoft Office 15\root\office15 (or Office16), or in C:\Program Files (x86)\Microsoft Office 15\root\office15 (or Office16) if using 32bit Office on 64-bit Windows.

With any Office version, if you use 64-bit Windows and 32-bit Outlook, selfcert will be in the 32-bit Programs directory.

In Windows 8 or 10, you can right click on the Outlook tile on the Start screen or Start menu and choose Open File Location (More > Open File Location in Windows 10). This opens Windows Explorer to the location of the Office shortcuts. Right click on the shortcut and again choose Open File Location to open Windows Explorer to the folder where Outlook.exe is. Type S to jump down then double click on SelfCert.exe)

It's simple to use: just type a name for your digital certificate then click ok.

Once you have the certificate, sign the macros using Tools, Digital Signature dialog and choosing a certificate. When you close Outlook, if you are asked to save the VBA project, click Yes, even if you saved it from the VBA editor.

Note that every time you edit the macro you'll need to resign it.

Using SelfCert Video Tutorial

 

Add Macros to the toolbar or ribbon

If you are going to be using a macro often, you will probably want to create a toolbar button for it.

In Outlook 2010 and up, you can add buttons to the QAT or the ribbon. Go to File, Options and choose Customize Ribbon or Quick Access Toolbar.
Customize Outlook 2010 and 2013 ribbon

You'll need to add a new Group to an existing ribbon tab or add a new tab and new group before selecting Macros from the Choose commands from menu. Select the macro and click Add to add it to the selected group in the right pane.

In Outlook 2007 ribbons: you can only add buttons to the QAT. Right click on a ribbon tab and choose Customize Quick Access Toolbar. Select Macros from the Choose commands from menu then select the macro and Add it to the right pane. (When you add buttons to the QAT you can use Alt+n keyboard shortcuts.)

In Outlook 2007 (main interface) and older versions of Outlook: right click on the toolbar area and choose Customize. Switch to the Commands tab and find Macros on the left.
macros-toolbar

Drag the macro from the right side to the toolbar and drop. Right click on it to edit the name and button image.

More Information

Need help with sample code? Try the forums at Outlookcode.com or the Microsoft programming forums at MSDN

Looking for sample code? Outlookcode and VBOffice.net have a large number of code samples.

How to use Outlook's VBA Editor was last modified: March 29th, 2018 by Diane Poremsky

Related Posts:

  • Shortcuts to open Outlook folders
  • Macros to send message using the default account or a specific account
  • Create Tasks from Email and move to different Task folders
  • Print a list of your Outlook folders

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

274
Leave a Reply

2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 
2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 

  Subscribe  
newest oldest most voted
Notify of
gavin
gavin

I have some vb that checks for missing attachments, checks that email isn't being sent to the sender and warns if certain accounts are being used to send emails. It works fine but only with all macros enabled, which seems to be a bit of a risk. I've self-signed it, saved it and reopened outlook but no matter what I do it only seems to run with security set to run all macros without warnings. I'm not an expert in this field so I'm a bit stuck. Any thoughts?

Vote Up00Vote Down Reply
July 1, 2018 2:00 pm
Diane Poremsky
Diane Poremsky

Unlike Word and Excel macros, Outlook is fairly safe - its difficult to add macros to outlook and the vb editors need to be opened to activate them if you copy the project file.

Which version of office are you using? Did you try making a new certificate? Did you trust the certificate?

Vote Up0-1Vote Down Reply
July 1, 2018 10:57 pm
daniel Patrick
daniel Patrick

Is it possible to run a macro to add a signature that you have setup so that it goes into the subject line?

Vote Up00Vote Down Reply
June 18, 2018 7:29 pm
Diane Poremsky
Diane Poremsky

You can use a macro to set the subject line.

Vote Up00Vote Down Reply
June 18, 2018 10:58 pm
Rajamahender Rapelly
Rajamahender Rapelly

Hi Diane,

Thank you for the information. I would like to save the invoices(attachments) in a pdf format to a repository which is on network of my company. Whenever I receive a new email, the code should save the file automatically. First it should find the vendor name present in the name of the attachment and second the code should open the pdf attachment and find the location under the section of "Bill To" which is present inside the invoice.

The repository should contain the main folder invoices and it should contain four sub-folders with the plant name. Under each plant name there should be sub-folders with vendor names. The received attachment should be save to respective plant folder and then to the vendor folder.

I am little confused and worried to do this as I am new to coding. I request you to help with this problem.

Thank you
Best Regards
Rajamahender Rapelly

Vote Up00Vote Down Reply
May 16, 2018 4:42 am
Diane Poremsky
Diane Poremsky

sorry I missed this earlier. It is doable, but complicated - you need to do a search at each folder level or hard code the paths down to the plant name.

Vote Up00Vote Down Reply
July 1, 2018 11:00 pm
Rajamahender Rapelly
Rajamahender Rapelly

Hi,

I would like to receive the vendor invoices to a dedicated email id and save the invoices automatically in the repository as per the plant location wise and vendor wise. The vendor should be identified by the name of the invoice and the plant location should be identified by the billing address present in the invoice. Based on this two factors: plant location first and then vendor name second, the invoice should be save automatically to a repository.

Could you tell me how to achieve this? I need the macros for this to implement this.

Thank you
Rajamahender Rapelly

Vote Up00Vote Down Reply
May 15, 2018 7:21 am
Diane Poremsky
Diane Poremsky

This should point in you in right direction https://www.slipstick.com/developer/code-samples/save-messages-and-attachments-in-a-new-folder/ - it creates the folder, but if you have folders are are using them for all messages or files based on the invoice and plant, that will work too.

Depending on how the invoice and plant are in the message, you may need ot use regex to find them - i have samples on the site that use that method.

Vote Up00Vote Down Reply
May 16, 2018 12:38 am
Ros
Ros

Hi Diane
Thanks so much for all of your tips and tricks. I've just migrated over to Outlook after a lifetime on Pegasus Mail, and I'm battling a bit to get the environment to operate the way I like it! I found a macro somewhere else to add a pop-up box after sending to ask whether to keep a copy or not. In order to get it to work, I had just disabled all macros, knowing it's not a good idea. Now you've taught me that I can sign the macro. I followed all your steps, but when I restart Outlook, it gives me an error "An error occured while attempting to verify the VBA project's signature. Macros will be disabled" Any ideas? Thanks so much,

Vote Up00Vote Down Reply
February 15, 2018 7:10 am
Diane Poremsky
Diane Poremsky

I didn't realize Pegasus was still around (I googled to check) - i thought it went the way of Eudora.

This is with Outlook 2016? Some people have this problem - its been hard to reproduce, which makes it hard to solve.

What version of Office are you using? Look in File, Office Account - what is the version/build #, include any words that follow the build #.

Vote Up00Vote Down Reply
February 15, 2018 9:16 am
Ros
Ros

Ahh, lovely Pegasus Mail. Maybe I should have stuck with it.... maybe I still will. At least I know how to import messages into it, whereas I can't seem to import my thousands of PMail messages and folders into Outlook in any cheap and painless manner....

Anyway: Office 2016, Version 1801 (Build 9001.2144Click-to-Run) Monthly Channel.

Thanks!

Vote Up00Vote Down Reply
February 15, 2018 12:29 pm
Diane Poremsky
Diane Poremsky

>> whereas I can't seem to import my thousands of PMail messages and folders into Outlook in any cheap and painless manner

Yeah... there are ways that will be fairly painless but not cheap. Outlook doesn't support importing other mail anymore (and never directly supported Pegasus) - you need to use a 3rd party utility. Address magic is pretty good IMHO.

>> Office 2016, Version 1801 (Build 9001.2144Click-to-Run) Monthly Channel.
That is the version i have - will try again to repro the error.

Honestly, if you are happy with Pegasus and don't need features found in Outlook, i would probably stick with it. Worst reason ever for switching is because you bought office and 'paid for Outlook' - i tell people to consider it a free app that came with the programs you use. :) (PowerPoint & Publisher are my 'free' apps.) But if you need specific feature you can only get in outlook... then it is probably worth the cost to migrate mail (if you can't store it in a universal format that can be opened outside of Pegasus or outlook).

Vote Up00Vote Down Reply
February 15, 2018 12:42 pm
Ros
Ros

There are little niggles in Pegasus Mail. Like sometimes I try to load images in a message and it hangs. Or when I reply to a message, some of the original message gets cut off. Granted, some of that may have been due to using a very old version. Formatting within a message can be a little clunky. You can't attach multiple files in one action. And then recently I needed to add another POP3 account, but there was an issue with it in terms of options of what to do with the mail on the server once it had been downloaded... I can't recall the details, but there was an option in Outlook that wasn't available in PMail that I needed. In the end I just added the account to my GMail and that sorted it out.

(PS I didn't pay for Office - a friend had paid for 5 copies and donated one to me)

Vote Up00Vote Down Reply
February 15, 2018 2:04 pm
Diane Poremsky
Diane Poremsky

LOL... so all of the apps are free. :)

Vote Up00Vote Down Reply
February 15, 2018 7:07 pm
Diane Poremsky
Diane Poremsky

BTW, if you need a new client but don't need outlook's features, try eM client. It might even be able to import the old mail. It can import Outlook, Outlook Express, Windows Mail, Live Mail, Thunderbird, The Bat, mbox…

Vote Up00Vote Down Reply
February 15, 2018 7:58 pm
David
David

Hi Diane
Thank you so very much for finally laying to rest my daily fight with opening templates.

Vote Up00Vote Down Reply
January 19, 2018 2:49 pm
Michael Lewis
Michael Lewis

Hello Diane; I was wondering if it was possible to use a macro to automatically add a duplicate calendar entry to a shared calendar?

Vote Up00Vote Down Reply
October 23, 2017 6:32 pm
Diane Poremsky
Diane Poremsky

Yeah, I should have one on the site - search for vba copy to calendar - when I get back in my office I’ll post the link.

Vote Up10Vote Down Reply
October 23, 2017 7:02 pm
Neil Jullette
Neil Jullette

Sub ChangeSubjectThenSend()
Dim myolApp As Outlook.Application
Dim aItem As Object

Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder

For Each aItem In mail.Items
aItem.Subject = "Testing dOGG"
aItem.Save

Set myForward = aItem.Forward
myForward.Recipients.Add "alias@domain.com"
myForward.Send

Next aItem
End Sub

Vote Up00Vote Down Reply
September 5, 2017 1:00 pm
Diane Poremsky
Diane Poremsky

Set myForward = aItem.Forward
myForward.Recipients.Add "alias@domain.com"
myForward = aItem.subject
myForward.Send

Vote Up00Vote Down Reply
September 13, 2017 12:54 am

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 24 Issue 3

Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Subscribe to Exchange Messaging Outlook






Our Sponsors

  • Popular
  • Latest
  • Week Month All
  • Adjusting Outlook's Zoom Setting in Email
  • The Signature or Stationery and Fonts button doesn't work
  • Security Certificate Warning in Microsoft Outlook
  • How to Remove the Primary Account from Outlook
  • This operation has been cancelled due to restrictions
  • Two Copies of Sent Messages in Outlook
  • Outlook's Rules and Alerts: Run a Script
  • Outlook is Not Recognized as the Default Email Client
  • To Cc or Bcc a Meeting Request
  • iCloud error: Outlook isn't configured to have a default profile
  • Outlook.com: Manage Subscriptions
  • Group By Views don’t work in To-Do List
  • Category shortcuts don’t work
  • How to disable the Group By view in Outlook
  • Adjusting Outlook's Zoom Setting in Email
  • Change the Subject of an Incoming Message
  • Creating Signatures in Outlook
  • Scheduling a Recurring Message
  • OneNote is missing from Office 365 / 2019
  • Create Rules using PowerShell
Ajax spinner

Newest VBA Samples

Adjusting Outlook's Zoom Setting in Email

Move email items based on a list of email addresses

Remove prefix from Gmail meeting invitations

How to hide LinkedIn, Facebook, Google and other extra contact folders in Outlook.com

Use VBA to create a Mail Merge from Excel

Open multiple Outlook windows when Outlook starts

Set most frequently used Appointment Time Zones

How to change the From field on incoming messages

VBA: File messages by client code

Update Contact Area Codes

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in Outlook for Windows.

Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Better Outlook Reminders?
  • Coming Soon to Windows 10: Office 365 Search
  • Outlook Links Won’t Open In Windows 10
  • BCM Errors after Upgrading to Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook 2016 Top Issues

  • The Windows Store Outlook App
  • Emails are not shown in the People Pane (Fixed)
  • Calendars aren’t printing in color
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • BCM Errors after October 2017 Outlook Update
  • Excel Files Won’t Display in Reading Pane
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

Outlook-tips.net Samples

VBOffice.net samples

OutlookCode.com

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes
  • “Live” Group Calendar Tools

Convert to / from Outlook

  • Converting Messages and Calendar or
    Address books
  • Moving Outlook to a New Computer
  • Moving Outlook 2010 to a new Windows computer
  • Moving from Outlook Express to Outlook

Recover Deleted Items

  • Recover deleted messages from .pst files
  • Are Deleted Items gone forever in Outlook?

Outlook 2013 Absolute Beginner's Guide

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Sync & Share

Share Calendar & Contacts

Synchronize two machines

Sharing Calendar and Contacts over the Internet

More Tools and Utilities for Sharing Outlook Data

Access Folders in Other Users Mailboxes

View Shared Subfolders in an Exchange Mailbox

"Live" Group Calendar Tools

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Outlook BCM | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Advertise | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2019 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

You are going to send email to

Move Comment