Getting Started With Outlook VBA
Getting Started With Outlook VBA
Getting Started Here are the absolute basics for getting started with Outlook VBA: 1. In Outlook 2000 to 2003, choose Tools | Macro | Security and set security to Medium. In Outlook 2007, the macro security settings are in the Tools | Trust Center dialog. Set macro security to Warn on all macros. 2. Restart Outlook. 3. Press Alt + F11 to bring up the VBA environment, 4. Expand the Project Explorer at upper left. 5. Double-click the built-in ThisOutlookSession module to open it. Accept the prompt to enable macros. 6. Type or paste your code into the ThisOutlookSession module. Here is a simple sample you can use to see if it's all working. It creates a new mail message with the subject "Hello World" and displays it: Sub HelloWorldMessage() Dim msg As Outlook.MailItem Set msg = Application.CreateItem(olMailItem) msg.Subject = "Hello World!" msg.Display Set msg = Nothing End Sub To run a VBA macro from the VBA environment, press F5 or click the Run button on the toolbar. Or run it from the main Outlook window with Alt-F8. If you get an "enable macros" prompt, choose to enable. About the Application Object Application is an intrinsic object that represents the running Outlook application. To avoid security prompts , you should derive all Outlook objects in your code from this intrinsic Application object, as in this statement that creates a new mail message: Set msg = Application.CreateItem(olMailItem) For an example of how to get a "trusted" MailItem object derived from Application when writing a procedure for use with an Outlook "run a script" rule action, see:
If you want to write a macro that operates on either an open item or an item selected in a folder, you can add the GetCurrentItem() function to your VBA project. Just remember to change this statement: Set objApp = CreateObject("Outlook.Application") to: Set objApp = Application so that it uses the intrinsic Application object. To process all the items selected in Outlook's main window, first return the ActiveExplorer.Selection collection: Set colSel = Application.ActiveExplorer.Selection To perform some action on the currently open item, first return the ActiveInspector.CurrentItem object: Set objItem = Application.ActiveInspector.CurrentItem
WARNING: I have seen cases where a machine running "run a script" rules completely loses the VbaProject.otm file that contains all the Outlook VBA code. If you use "run a script" rules, be sure to back up VbaProject.otm regularly or export the individual modules. Method 2: Using the Items.ItemAdd event This approach uses the ItemAdd event to convert all items arriving in the Inbox to plain text format. It needs to run in the built-in ThisOutlookSession module in Outlook VBA. Option Explicit Private WithEvents olInboxItems As Items Private Sub Application_Startup() Dim objNS As NameSpace Set objNS = Application.Session ' instantiate objects declared WithEvents Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items Set objNS = Nothing End Sub Private Sub olInboxItems_ItemAdd(ByVal Item As Object) On Error Resume Next Item.BodyFormat = olFormatPlain Item.Save Set Item = Nothing End Sub This sample monitors only the Inbox folder. You could do much the same thing with other folders by adding code to do the following: Declare an Items collection object for each folder WithEvents Instantiate each Items collection object in the Application_Startup event handler Add an ItemAdd event handler for each Items collection object If the folder you want to monitor is a default folder, you can use the Namespace.GetDefaultFolder method to return it as a MAPIFolder object (Folder in Outlook 2007). To monitor a default folder in another person's Exchange mailbox, use Namespace.GetSharedDefaultFolder to get the MAPIFolder or Folder. Otherwise, you can use my GetFolder() function to walk the folder hierarchy and return the MAPIFolder or Folder corresponding to a given path string. The Items.ItemAdd method will not fire if more than 16 items arrive in a folder at one time. Method 3: Using the NewMailEx event
The NewMailEx event fires for messages arriving on any email account. Unexpectedly, for messages arriving from an Exchange account, the NewMailEx event returns only one EntryID value each time it fires. Private Sub Application_NewMailEx _ (ByVal EntryIDCollection As String) Dim arr() As String Dim i As Integer Dim ns As Outlook.NameSpace Dim itm As MailItem Dim m As Outlook.MailItem On Error Resume Next Set ns = Application.Session arr = Split(EntryIDCollection, ",") For i = 0 To UBound(arr) Set itm = ns.GetItemFromID(arr(i)) If itm.Class = olMail Then Set m = itm m.BodyFormat = olFormatPlain m.Save End If Next Set ns = Nothing Set itm = Nothing Set m = Nothing End Sub For a POP3 account running under a very heavy load, it may skip a few items, but still is more reliable than Items.ItemAdd. If it is absolutely critical that you process all incoming items, it would be a good idea to supplement NewMailEx with a timer-based routine that checks periodically for unprocessed messages. Notes Changing a message from HTML to plain text makes any embedded image attachments inaccessible from the user interface. They are still stored in the message, however, and can be accessed via code. The Application.NewMail event is not very useful in processing new mail, as it tells you only that messages have arrived -- not which ones nor which folder(s) they might be in.