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.
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.
Leave a Reply
266 Comments on "How to use Outlook's VBA Editor"
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,
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 #.
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!
>> 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).
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)
LOL... so all of the apps are free. :)
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…
Hi Diane
Thank you so very much for finally laying to rest my daily fight with opening templates.
Hello Diane; I was wondering if it was possible to use a macro to automatically add a duplicate calendar entry to a shared calendar?
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.
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
Set myForward = aItem.Forward
myForward.Recipients.Add "alias@domain.com"
myForward = aItem.subject
myForward.Send
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?
Use myForward = aItem.subject befopre the myforward.send to fix the subject
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?
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.
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?
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.
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 ?
have you tried removing the certificate and adding it back?
Can you send me a screenshot of the dialog? (diane at slipstick)
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
Annoying, isn't it? It's supposed to stick, but I'm not aware of any way to force it when it doesn't. :(
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.
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.