0% found this document useful (0 votes)
921 views

Getting Started With Outlook VBA

This document provides three methods for processing incoming emails in Outlook VBA: 1. Using an Outlook rule with the "run script" action to call a VBA subroutine that processes emails meeting the rule conditions. 2. Using the Items.ItemAdd event to process emails as they are added to a specific folder like the Inbox. 3. Using the NewMailEx event to process emails across all accounts as they arrive.

Uploaded by

Asur100
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
921 views

Getting Started With Outlook VBA

This document provides three methods for processing incoming emails in Outlook VBA: 1. Using an Outlook rule with the "run script" action to call a VBA subroutine that processes emails meeting the rule conditions. 2. Using the Items.ItemAdd event to process emails as they are added to a specific folder like the Inbox. 3. Using the NewMailEx event to process emails across all accounts as they arrive.

Uploaded by

Asur100
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

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:

How to process incoming messages in Microsoft Outlook

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

How to process incoming messages in Microsoft Outlook


While Outlook has a feature to allow all messages to be received and sent in plain text format, sometimes you might want to convert only selected incoming messages to plain text. Such a conversion also makes a useful demonstration for three different VBA approaches for processing incoming messages: Using an Outlook rule Using the Items.ItemAdd event on a specific folder Using the NewMailEx event Method 1: Using an Outlook rule Starting with Outlook 2002, you can call a VBA subroutine from a Rules Wizard rule using the "run a script" rule action. This action calls not a "script" but a VBA procedure. The procedure can be in either the built-in ThisOutlookSession module or any regular (not class) code module. It must include either a MailItem or MeetingItem argument. This short example converts all incoming messages that meet the conditions for the rule to plain text by changing the value of the BodyFormat property: Sub ConvertToPlain(MyMail As MailItem) Dim strID As String Dim objMail As Outlook.MailItem strID = MyMail.EntryID Set objMail = Application.Session.GetItemFromID(strID) objMail.BodyFormat = olFormatPlain objMail.Save Set objMail = Nothing End Sub A "run a script" rule is not a good choice for heavy traffic applications, as Outlook is likely to skip applying the rule if too many items arrive that meet the rule's conditions. To avoid security prompts when accessing properties like Body and Recipients, you must use the technique above to get the item indirectly, through its EntryID property and the Namespace.GetItemFromID method (or Application.Session object). If you attempt to access MyMail.Body or MyMail.SenderName, for example (MyMail being the name of the parameter for the "run a script" rule procedure), you will get a security prompt. Generally, items that you want to process by a "run a script" rule should not be processed by other rules or other actions in the same rule. Put that rule high on the list, and include the "stop processing" action. Include in your VBA procedure all the actions you want to take on the items that meet the rule's conditions. 3

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.

You might also like