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

How to use Outlook's VBA Editor

Slipstick Systems

› Developer › How to use Outlook's VBA Editor

Last reviewed on March 29, 2018   —  266 Comments

July 29, 2011 by Diane Poremsky 266 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
  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Share on Skype (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to print (Opens in new window)

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.

Leave a Reply

266 Comments on "How to use Outlook's VBA Editor"

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
Ros
Ros
Share On TwitterShare On Google

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
Share On TwitterShare On Google

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
Share On TwitterShare On Google

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
Share On TwitterShare On Google

>> 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
Share On TwitterShare On Google

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
Share On TwitterShare On Google

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

Vote Up00Vote Down Reply
February 15, 2018 7:07 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

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
Share On TwitterShare On Google

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
Share On TwitterShare On Google

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
Share On TwitterShare On Google

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 Up00Vote Down Reply
October 23, 2017 7:02 pm
Neil Jullette
Neil Jullette
Share On TwitterShare On Google

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
Share On TwitterShare On Google

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
Neil Jullette
Neil Jullette
Share On TwitterShare On Google

I am using the below Script to change the subject line and send to a particular email. However this must be done without the "FW:" Is there anyone that can help me adjust this script to be ReDirect versus Forward?

Vote Up00Vote Down Reply
September 5, 2017 12:59 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

Use myForward = aItem.subject befopre the myforward.send to fix the subject

Vote Up00Vote Down Reply
September 13, 2017 12:53 am
Brian Vogel
Brian Vogel
Share On TwitterShare On Google

Hello Diane,

I'm back again with another question regarding running a script against incoming e-mail messages to activate a specific link.

I have all the code in under Outlook 2016 and did have to do the registry edit to get the "run a script" option to appear in rules.

I am positive that the VBA script/macro was self-signed with a certificate we obtained via selfcert.

When the script is being triggered the attached error message box is being presented:
Any theories on why and how to fix it?

VBA_Error_Message.jpg
Vote Up00Vote Down Reply
August 9, 2017 2:03 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

I think a recent update broke something - you aren't the only one with issues with self signed macros. The only advice right now is to use the lowest setting and remove the signing.

Vote Up00Vote Down Reply
August 9, 2017 4:12 pm
Brian Vogel
Brian Vogel
Share On TwitterShare On Google

Diane,

Thanks. We're giving this a try under Outlook 2016 and I am also doing the same on my machine with Outlook 2010.

I am almost literally ready to pull my hair out over what's not happening on my machine. I have the script in and a rule set up. If I run the debugger using the testing launch subroutine the script works fine. If I go through the rule again as though I were trying to change it, and say apply to all messages already in the inbox it starts flying right along and opening tab after tab after tab with the URL I've specified. But it will NOT do the same thing when a message actually arrives.

What on earth could I be doing wrong?

Vote Up00Vote Down Reply
August 9, 2017 10:23 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

Follow Up for others with the same problem - This is a limitation of imap accounts - the body is not downloaded immediately. An itemadd macro should work or trigger a download in VBA first - see this solution at the end of this thread at outlookforums.

Vote Up10Vote Down Reply
August 16, 2017 5:39 pm
philip
philip
Share On TwitterShare On Google

Hi, I think I have the same problem. I have a vba script that has been running fine for months. I have a self signed cert. It suddenly stopped a couple of days ago. no I get the allow deny dialog and allow is disabled. Any ideas ?

Vote Up00Vote Down Reply
October 11, 2017 7:57 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

have you tried removing the certificate and adding it back?
Can you send me a screenshot of the dialog? (diane at slipstick)

Vote Up00Vote Down Reply
October 12, 2017 8:06 am
knw
Share On TwitterShare On Google

Hi Diane,
Every time I open the Outlook VBE, I add the Edit toolbar. Every time I close Outlook and then reopen it, and open VBE, the Edit toolbar has disappeared again.
Why does it do this, and can I fix it?

I'm running VBA for Apps 7.1
Outlook Pro Plus 2013

Vote Up00Vote Down Reply
June 21, 2017 9:00 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

Annoying, isn't it? It's supposed to stick, but I'm not aware of any way to force it when it doesn't. :(

Vote Up00Vote Down Reply
June 24, 2017 10:39 pm
Bolaji Akerele
Bolaji Akerele
Share On TwitterShare On Google

Hi Diana
I designed a Macro on 2013 Microsoft Outlook and Signed the Macro with a Certificate from an accredited CA. However, whenever I try to run it, it returns an error that Macros as been diabled as it seems to have been tampered with. I have tried this multiple times and even rebuilt the macro but the error persists.
Kindly assist.
NB: I would have tried the Trusted location but I observe the option does not exist for Outlook.
I run office 2013 64bits also.

Vote Up1-1Vote Down Reply
May 31, 2017 7:00 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

if you edit a macro after signing it, you need to remove the signature save it, then re-sign it (approx. 2:20 in the video). Try removing the signature from the macro, save, then close Outlook. If asked to save vba again, click yes. Reopen outlook & the vba editor. Sign the macro and Save. Close outlook and Save if asked again.

Outlook doesn't use a trusted location for macros - all macros are in the visual basic project file.

Vote Up1-3Vote Down Reply
May 31, 2017 11:21 am

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

Latest EMO: Vol. 23 Issue 10

Subscribe to Exchange Messaging Outlook






Our Sponsors

  • Popular
  • Latest
  • Week Month All
  • This operation has been cancelled due to restrictions This operation has been cancelled due to restrictions
  • Adjusting Outlook's Zoom Setting in Email Adjusting Outlook's Zoom Setting in Email
  • How to Remove the Primary Account from Outlook How to Remove the Primary Account from Outlook
  • Pictures Don't Display in Outlook Messages Pictures Don't Display in Outlook Messages
  • Outlook is Not Recognized as the Default Email Client Outlook is Not Recognized as the Default Email Client
  • To Cc or Bcc a Meeting Request To Cc or Bcc a Meeting Request
  • Remove a password from an Outlook *.pst File Remove a password from an Outlook *.pst File
  • The Signature or Stationery and Fonts button doesn't work The Signature or Stationery and Fonts button doesn't work
  • Understanding Outlook's Auto-Complete Cache (*.NK2) Understanding Outlook's Auto-Complete Cache (*.NK2)
  • Exchange Account Set-up Missing in Outlook 2016 Exchange Account Set-up Missing in Outlook 2016
  • iCloud error: Outlook isn't configured to have a default profile iCloud error: Outlook isn't configured to have a default profile
  • Setting Custom Reminder Times Setting Custom Reminder Times
  • Add Additional Addresses to Room Mailboxes Add Additional Addresses to Room Mailboxes
  • Create a Task from a Message and include the Attachment Create a Task from a Message and include the Attachment
  • Forward email messages by date Forward email messages by date
  • Open multiple Outlook windows when Outlook starts Open multiple Outlook windows when Outlook starts
  • Office 365 Fraud Detection Checks Office 365 Fraud Detection Checks
  • Outlook Request: Calendar Details View Outlook Request: Calendar Details View
  • Outlook's "Not Junk" option isn't available Outlook's "Not Junk" option isn't available
  • Outlook Tip: Show all Mondays in the Calendar Outlook Tip: Show all Mondays in the Calendar
Ajax spinner

Newest VBA Samples

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

Set a reminder on selected items in the To-Do List

Replicate GTD: Create a task after sending a message

Use VBA to read fields in attached messages

Move Outlook Folders using VBA

Replicate Smart Lookup using a macro

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 © 2018 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

You are going to send email to

Move Comment