Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


January 2000

Automatic Message Processing


RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

End of Article



Reader Comments
The URL was not corret.....

http://www.microsoft.com/office/enterprise/prodinfo/supreasy.htm


Would you mind to check it out and email it bac to me?

Thx


Philip Wong December 27, 1999


This is the URL
http://www.microsoft.com/office/outlook/SuprEasy.htm

Anthony G. Allen Sr. July 12, 2000


This only works on the first message of a block message transfer. If you move five messages from one folder into the inbox, only the first item is passed. Same on startup. This is a known issue with the Exchange architecture that Microsoft has acknowledged.

Jason January 16, 2003


>> Jason wrote:
>> This only works on the first message of a block message transfer. If you
>> move five messages from one folder into the inbox, only the first item is
>> passed. Same on startup. This is a known issue with the Exchange
>> architecture that Microsoft has acknowledged....

Jason,
Could you cause the new items to all be selected, then use a For... Next loop on .ActiveExplorer.Selection.Count?

Dim oOL As Outlook.Application
Set oOL = CreateObject("Outlook.Application")
For x = 1 To oOL.ActiveExplorer.Selection.Count
'Process your selected items here
Next x

That should allow all new items to be selected, and since they're selected, they'll be processed.

Darren Brothers January 29, 2004


current url: http://www.microsoft.com/office/previous/outlook/supreasy.asp

Anonymous User November 02, 2004 (Article Rating: )


Hi,

I am an account by trainig and have very limited knowledge of VBA.

However, I would like to know what the code is to email a specified attachment to specified users.

Any help would be greatly appreciated. Send to scott.haley@fxall.com

Anonymous User November 18, 2004


I am very new to coding and would like to know how involved is writing a code to collect the sent items from a mail box and deliver them to another folder in another mailbox?

Anonymous User December 05, 2004 (Article Rating: )


is it possible to automatically put selected items in a PST file?

Anonymous User April 13, 2005 (Article Rating: )


How can I clear the new e-mail icon in the notification area of the desktop for unwanted e-mail. I use Outlook 2003 on a Windows 2000 Pro computer.

Anonymous User August 18, 2005 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Confirmed: Battery Life Issues Not Windows 7's Fault

Microsoft on Monday issued a lengthy statement about the recent Windows 7 battery controversy, echoing my assessment from earlier in the day, but backing it up with hard, cold evidence. ...

Battery Life Issues Almost Certainly Not Windows 7's Fault

While Microsoft is still investigating a notebook battery life issue that was supposedly caused by Windows 7, some interesting trends have emerged. ...

Microsoft Warns of Windows Version Expirations

Microsoft warned that this year will see three out-of-date Windows versions slip into retirement. ...


Task Automation Whitepapers Three Ways to Prevent Insider Risk within Your SCCM-Managed Environment

From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Top 5 Key Technologies Changing The Face of Exchange and Data Protection

Deep Dive into Windows Server 2008 R2 presented by John Savill

Bail Out Your Exchange Environment

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2010 Penton Media, Inc. Terms of Use | Privacy Statement