Writing VBA code for Microsoft Outlook
Visual Basic for Applications (VBA) is one of two programming languages available for writing code in Outlook. (The other is VBScript, which is used by Microsoft Outlook custom forms. You can also automate Outlook using VBA in other Outlook applications. Even developers who program in other languages, such as VB.NET and C# from Visual Studio, will find Outlook VBA useful for exploring Outlook's capabilities and prototyping. Among the useful procedures that you can write with Outlook VBA are:
- Macros to run code on demand by pressing Alt+F8 or by adding the macro as a custom toolbar button
- "Run a script" rule procedures that can be used to process incoming messages and meeting requests
- Event handlers to respond to the user's interaction with Outlook or with such events as Reminders.ReminderFire
Getting started with Outlook VBA
To launch VBA in Outlook, press Alt+F11. Macro security is set to High by default, which means that unsigned projects will not run. You can change macro security with the Tools | Macro | Security command. Also, you can use the Selfcert.exe program that comes with Office to generate a local security certificate for signing the project. For more pointers on getting started with Outlook VBA, see:
The key to using VBA with Outlook is understanding the Outlook Object Model, which defines what objects, properties, methods, and events are available to your code. The object model is the same for every language. Therefore, the information here on Outlook techniques can be applied to any code environment that uses Outlook objects.
Any code that you create in the Outlook VBA environment is stored in a file named VBAProject.otm. The location of this file depends on your exact Windows setup; see Outlook & Exchange/Windows Messaging Backup and Dual-Boot.
When you first open Outlook VBA, it will already have a built-in class module named ThisOutlookSession which supports an intrinsic Application object and its events. In addition to backing up the VBAProject.otm file, you might also want periodically to export the individual modules from the project and back them up, too.
You can copy the VBAProject.otm file to another user's machine but the macros won't run until the user has actually used one of the Tools | Macro commands. See Distributing Microsoft Outlook VBA Code for other thoughts and techniques on distributing VBA macros. Even though you can copy the VBAProject.otm file, it's still not a supported method for distributing Outlook macros company-wide. A better method is to create an Outlook add-in.
Key Outlook VBA Techniques
Outlook developers commonly work with items in collections, such as the Folders and Items collections. These articles provide useful pointers and samples:
"Object model guard" security prompts
Always derive all Outlook objects in VBA from the intrinsic Application object. In most cases, that will ensure that your code doesn't trigger security prompts. If you are building your own VBA code by adapting a sample that creates an Outlook object with CreateObject("Outlook.Application"), you don't need that statement. Use the intrinsic Application object instead.
For more information on the Outlook "object model guard" prompts, see:
Working with toolbars and menus
General VBA techniques
If you can't get VBA to run at all, see:
For version-specific issues, including fixes in various service packs, see:
If you add a macro to a toolbar or menu, but it doesn't execute when you click the toolbar button, make sure that the macro subroutine has name that is different from the code module's name. You might want to write all your toolbar macros in a module named ToolbarMacros.
From other resources:
Updated free tool for digging deeply into the Outlook & Exchange folder and item structure. See Announcing MAPI Editor (Formerly MFCMAPI) for what's new.
Free COM component designed to read MAPI-properties of CDO and Outlook Object Model objects for Microsoft Outlook 2000, 2002/XP, 2003 without triggering security prompts. Microsoft Visual C++ 6.0 source code included.
Inexpensive developer library that enhances the events available for the Items and Folders collection to provide more reliable Add and Change events and Remove events that include the EntryID of the removed item or folder.
Provides an interface to Outlook objects that avoids the "object model guard" of the Outlook E-mail Security Update and exposes properties and methods not available through the Outlook model, such as sender address, the RTF body of an item, and Internet message headers. Several security features protect it from being used by malicious programs to send Outlook mail. For the redistributable version, it adds a Profman.dll component with the ability to enumerate, add, delete, and modify Outlook mail profiles using VB or VBScript.
Developer utility for finding out what's going on inside Outlook, via the Outlook object model, CDO and MAPI. You can edit and delete most properties, drag properties from one item to another, copy values to the clipboard, run scripts, monitor events, browse toolbars to get CommandBars IDs.
This tool comes with Office so you can add a digital signature to your VBA project. Once you run it, restart Outlook and in the VBA window, choose Tools | Digital Signature, then click Choose to sign your project with the new certificate. Also see:
Code Signing Office XP Visual Basic for Applications Macro Projects
OFF2000- Using SelfCert to Create a Digital Certificate for VBA Projects
Office XP Macro Security White Paper
HOW TO - Add a Digital Signature to a Custom Macro Project in Office 2003 and Office XP
Office Automation and Digital Certificates Demonstration