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


April 2000

Moving Incoming Items to Folders


RSS
Subscribe to Windows IT Pro | See More Visual Basic (VB) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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 Folders\All Public Folders\IT\Important. 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 <folderobject> Is Nothing
   Then
   ' <code continues>
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.

End of Article



Reader Comments
Hi,

Great article Sue! And well set out, with easy downloads etc.
I wanted to e-mail you to ask for advice on writing a macro for Outlook2000 that would alter the settings in the Options for an e-mail message I was about to send. I want to alter the filed that defines where replies should be sent.
I can write the code to display the options Dialog box, but cannot see how to "fillin" the fields.

Any suggestions/pointers?


Les Landau March 28, 2000


<i>See http://www.win2000mag.com/Articles/Index.cfm?ArticleID=15442 for more VBA code that defines where replies should be sent, in response to Les' comment.</i><br><br>
--Sue Mosher

Sue Mosher April 20, 2001


Have you tried 'Move' method for mail items in Looping??
e.g. the selected mail itmes are hundreds by some criteria.
Does it work well??

liam May 18, 2001


Does this alsow work for Public Folders?

Peter December 13, 2003


Hi Sue
I placed the above code in outlook 2002 - created the subfolder as you mentioned, however nothing happens when I send any mail with Importance. Why? What am I doing wrong - is ther code I am missing or wont it work on 2002?

Please advise.



ross January 22, 2004


I have been trying to find a method of automatically updating a shared calendar in Public folders from items in a group of peoples personal calendars. This will save maually copying items to the shared folder. I can't find anything anywhere. HELP ???????

Thanks



Kevin Timmis February 17, 2004


Ross, did you check your macro security in Tools | Macros | Security? If it's High, you'll need to lower it or use the selfcert.exe to create a certificate and sign your VBA project.

Sue Mosher March 03, 2004


Sue, I'm looking for the option to move eml file from public folders to a mailbox, is it possible?

David April 18, 2004


Article was useful

deepan July 16, 2004 (Article Rating: )


I want to write a macro in Outlook to forward automatically an email to another address found in the email body.

deepan July 16, 2004 (Article Rating: )


 See More Comments  1   2 

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. ...

Microsoft Warns of Windows Version Expirations

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

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. ...


Exchange Server and Outlook Whitepapers Email Controls and Regulatory Compliance

Take Control of Your Email: Understand the Business Reasons for Email Storage Management

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!

Exchange Server and Outlook eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

The Expert's Guide for Exchange 2003: Preparing for, Moving to, and Supporting Exchange Server 2003

Related Exchange Server and Outlook 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

Exchange & Outlook UPDATE eNewsletter
News, strategies, products, and developments in Exchange Server and Outlook messaging.

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