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.
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.
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.
- Go to Tools, References menu.
- 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.)
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.)
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.
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.
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.
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.
Thank you! My signing cert expired and this help me get a new one and resign my macro!
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
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?
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???
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?
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?
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"
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.
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?