Moving Incoming Items to Folders

Find out how to check for important incoming messages and move those messages from the Inbox to another folder.

Sue Mosher

February 22, 2000

3 Min Read
ITPro Today logo in a gray background | ITPro Today


Moving Incoming Items to Folders

This column is the fourth in a series about how to use Visual Basic for Applications (VBA) in Outlook 2000 to duplicate Outlook Rules Wizard features. This time, I look at two skills—checking for important incoming messages and moving those messages from the Inbox to another folder (i.e., an Inbox subfolder or a folder in Public Folders to which you have permission to add items).

Create an Inbox subfolder by choosing File, New, Folder. Give the folder the name Important. You'll probably want to add the folder to the Outlook Bar so that you can easily see the number of unread items. Next, put the code you see in Listing 1 in the ThisOutlookSession module of the Outlook VBA window. (As I explained in earlier installments of this column, this code sets up the Inbox so that VBA can monitor it for new items.) Finally, place the code you see in Listing 2, for the ItemAdd event handler, in ThisOutlookSession. This procedure runs when a new item enters the Inbox. Let's look more closely at this procedure.

After determining whether the new item is a message (e.g., not a meeting request), the ItemAdd event handler uses the statement

If Item.Importance = olImportanceHigh Then

to check whether the new item is of high importance (i.e., marked with a red exclamation point in an Outlook view). If the item is important, the code uses a series of Set statements to return a Namespace object and a MAPIFolder object that represents the Inbox. (Compare these statements with the similar lines in Listing 1.) The third Set statement obtains the MAPIFolder object that represents the Important subfolder you created:

Set objImportantFolder =    objInbox.Folders("Important")

This Set statement takes advantage of the fact that a folder contains not only an Items collection that represents its Outlook items but also a Folders collection that represents its subfolders. You can obtain any subfolder, as long as you know its name. (In a moment, I show you how to similarly obtain a subfolder of a subfolder of a folder in Public Folders.)

The code then checks to make sure you have a valid folder object:

If Not objImportantFolder Is Nothing Then

If a valid folder object exists, the code moves the item to the target folder. The key to using the Move method on an Outlook item is to first obtain a valid folder object.

To move important items to a folder in Public Folders, rather than one in your mailbox, you could instead use the code you see in Listing 3. This code assumes that the Important folder is a subfolder of the IT folder, which is a top-level folder that the IT department might use to organize its public folders. The full path to the Important folder would therefore be Public FoldersAll Public FoldersITImportant. In programmer terminology, Listing 3 walks the folder tree, using the Folders collections to specify each subfolder by name, starting with the Folders collection on the Namespace object, until the code returns the final objImportantFolder object.

Notice the pair of On Error statements in Listing 3. The first statement turns off error handling, instructing Outlook to ignore any errors and execute the next line of code if an error occurs. The second statement restores error handling; any subsequent errors will result in a message to the user.

These On Error statements are necessary in case the Set statements find that a folder isn't present—a problem that might occur if the user is working offline or using a profile that doesn't include the Exchange Server service. Using the two On Error statements is simpler than adding for each folder a block such as

If Not  Is Nothing   Then   ' End If

to check whether a valid MAPIFolder object is present.

You can now combine this column's code with the code that I introduced in the March 2000 column ("Handling Blank Fields") to build a junk mail handler that moves suspected junk mail into a separate folder for later review.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like