• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

How to use Outlook's VBA Editor

Slipstick Systems

› Developer › How to use Outlook’s VBA Editor

Last reviewed on June 19, 2021     311 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.

 

Set a reference to other Object Libraries

If you receive a "User-defined type not defined" error, you need to set a reference to another object library.

  1. Go to Tools, References menu.
  2. Locate the object library in the list and add a check mark to it. (Word and Excel object libraries version numbers will match Outlook's version number.)
    Reference the Word object model in Outlook's VBA Editor

Commonly used libraries are Microsoft Word, Microsoft Excel, Microsoft Forms, and VBScript Regular Expression.

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: June 19th, 2021 by Diane Poremsky

Related Posts:

  • Macros to send messages using a specific account
  • Shortcuts to open Outlook folders
  • Reset reminders closer to Meeting time
  • Count Selected Messages

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.

Subscribe
Notify of
311 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Spencer (@guest_220822)
January 5, 2024 9:56 pm
#220822

Thank you! My signing cert expired and this help me get a new one and resign my macro!

0
0
Reply
Jay Burgess (@guest_220286)
May 17, 2023 4:23 pm
#220286

I answered my own question relative my version of Outlook (Outlook for Microsoft 365 MSO (Version 2304 Build 16.0.16327.20200) 64-bit.

The line and subroutine below go into a "Class Module", and then my macro subroutines all go into a "Module" module. The comment about "ThisOutlookSession" seems to have changed a little?

Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()
...
End Sub

0
0
Reply
Jay Burgess (@guest_220284)
May 17, 2023 10:25 am
#220284

My Outlook VBA macro library got corrupted, so I'm rebuilding it from a text file backup. A "WithEvents" statement and/or my startup subroutine generates a "Compile error: Only valid in object module" error. Google'ing seems to imply that I put my previous module under "ThisOutlookSession" instead of "Modules"? If so, I've tried everything in the VBA editor to create a module under "ThisOutlookSession", but "Insert / Module" always creates it under "Modules". What's the right way to re-add my code into "ThisOutlookSession", and hopefully resolve my error?

0
0
Reply
Shameka Harris (@guest_219884)
November 26, 2022 8:25 pm
#219884

Hello, I enabled the macro that copies from one calendar to another, but it is not showing on my macro list. How do I get it to show up???

0
0
Reply
Kay (@guest_219771)
October 6, 2022 11:16 am
#219771

I inserted a module, put in the code, named the module, used selfcert to create a certificate, enabled signed macros (even tried enabling all macros), but still NOTHING shows in the macro box. I am using Outlook 365 in Windows 11 with Exchange. Any ideas as to what I might be doing wrong?

0
0
Reply
Pedro Alves (@guest_219701)
September 9, 2022 6:22 am
#219701

Hi Diane, first of all, thank you very much for what you do.
I learned a lot, and continue to use a lot of your examples of VBA code in my professional life.

I need your help in an Outlook VBA code, I browsed through forums, but couldn't find a solution to this.
Probably super simple.

I'm using this code to split the name of the sender, and when I hit reply, it populates the body of the email with "Hi + strName".

strName = Split(Split(olReply.To & ";", ";")(0), " ")(0)

Now, how can I get the last element of the split name?

I know it's on the last 0, but don't know how to get there. You see because some people have 3 names, and some have only 2.

And I would like to get always the last index since the order of names is Surname (MiddleName, sometimes) FirstName.

Can you help?

0
0
Reply
Hornblower409 (@guest_219600)
August 5, 2022 9:02 pm
#219600

In the section "Using SelfCert to sign a macro" you say "selfcert is usually at C:\Program Files\Microsoft Office\OfficeXX".

I believe that (unless you are using 64 bit Outlook) the correct path would be : "C:\Program Files (x86)\Microsoft Office\Officexx"

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Hornblower409
August 5, 2022 11:09 pm
#219601

It would be in the same folder as the rest of the office apps - so, yeah, for 32 bit on 64bit windows, it would be in the x86 path. I wrote it back when 32-bit was still the thing.

0
0
Reply
Hornblower409 (@guest_219599)
August 5, 2022 8:55 pm
#219599

In the section "Using SelfCert to sign a macro" you say "Note that every time you edit the macro you'll need to resign it."

I have not found this to be true in my Outlook 2010. Once I have signed my project it does not require me to resign it no matter how extensive the changes. I have to assume you are right and I am wrong. So, what am I missing?

0
0
Reply

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

Latest EMO: Vol. 30 Issue 19

Subscribe to Exchange Messaging Outlook






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?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
Ajax spinner

Recent Bugs List

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

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

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

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

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

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

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

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

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

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

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment