Automatic Message Processing
Even before you roll out Outlook 2000 to your users, it can be an essential tool on your desktop.
November 23, 1999
If you're a Microsoft Exchange Server administrator, Microsoft Outlook 2000 can be an important tool on your desktop, even if you haven't rolled out Outlook 2000 to your users. Because Outlook 2000 has built-in Visual Basic for Applications (VBA) capabilities, you can easily write small applications that run either automatically within Outlook or from an Outlook toolbar button. For example, if you handle a variety of incoming messages (e.g., nondelivery reports—NDRs, user requests, directives from higher management), Outlook's Rules Wizard might not provide sufficient tools for organizing your messages. To help you become more productive, Outlook 2000 lets you write VBA code to automatically handle your own incoming messages.
Outlook 2000 supports automatic message processing by supporting program-level events. (Outlook 97 and Outlook 98 support only item-level events that fire when you use a custom form to work with a particular Outlook item.) For example, you can now write code for the ItemAdd event, which fires when a folder receives a new item.
In this first Outlook VBA on Demand column, I want to show you how to set up Outlook 2000 to respond to new messages in the Inbox and new tasks in the Tasks folder. However, because a full message-handling application is a complicated job that involves applying potentially complex conditions and actions to messages, I can't cover the entire subject in this first article. To start, I show you how to automatically categorize tasks that contain a specific word in the Subject field.
Most of the Windows programming environment is event-driven. Code runs only when certain events occur inside the program. In my example, I highlight two events: the Startup event on the Application object that fires when you load Outlook 2000, and the ItemAdd event that occurs when you add a new item to an Outlook folder.
To make Outlook 2000 respond to application events, you need to write code in ThisOutlookSession, a built-in module under Microsoft Outlook Objects in the VBA window. (Press Alt+F11 to open the Outlook VBA programming environment.) Listings 1 and 2 show the code you need to add to ThisOutlookSession.
To make Outlook 2000 react to new items in a folder, you need to work with the folder's Items collection. At callout A in Listing 1, you use the WithEvents keyword to declare two Items objects: One object represents items in the Inbox folder, and the other object represents items in the Tasks folder. Using WithEvents to declare objects in a class module such as ThisOutlookSession, you can write code for the events that those objects support.
For more information about the Outlook object model, press F2 in the Outlook VBA Window to access the Object Browser. Select any object, then press F1 to see a Help topic that explains the object and provides code examples. I also recommend the "Super-Easy Guide to the Microsoft Outlook Object Model" white paper at http://www.microsoft.com/office/enterprise/prodinfo/supreasy.htm and Micro Eye's Outlook Model Map at http://www.microeye.com/resources/objectmodel.htm.
Callout B in Listing 1 uses an event handler for the Startup event for ThisOutlookSession's intrinsic Application object. This event, which runs every time Outlook 2000 starts, sets the Items object variables to the items in the folders you want to monitor. (When Outlook 2000 shuts down, the Application object's Quit event sets those object variables to Nothing.)
The objTaskItems_ItemAdd subroutine, which callout A in Listing 2 shows, is an event handler that processes the ItemAdd event that fires when you add a new item to the Tasks folder. The subroutine uses the Instr() function to check whether the Subject property contains the text Idea:. If the Subject property contains the text, the subroutine updates the item's Categories property to include the Idea category.
What makes this procedure so simple is that the ItemAdd event returns as one of its arguments an object named Item that represents the new item you added to the folder. In other words, your routine already knows what item it needs to work with. Your code must simply tell Outlook 2000 what to do with the item.
I've also included an event handler for the ItemAdd event on the Inbox folder (the objInboxItems_ItemAdd subroutine), but I won't add code to it until next time. The event handler is where you'll write code that reacts to items entering the Inbox (thereby building a replacement for the Rules Wizard). However, you'll first need a few new Outlook VBA skills. You'll need to know how to check an incoming item for the conditions you're accustomed to setting in the Rules Wizard, and you'll need to know how to perform actions such as forwarding, sending, and moving items. I'll dig into those skills in the next Outlook VBA on Demand.
About the Author
You May Also Like