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.
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?
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?
Is it possible to run a macro to add a signature that you have setup so that it goes into the subject line?
You can use a macro to set the subject line.
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
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.
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
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.
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