Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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 log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


Exchange Server and Outlook Whitepapers Protecting (You and) Your Data with Exchange Server 2007

StoreVault SnapManagers for Microsoft Exchange and SQL Server

Related Events The Myths & Truths of Email Management with SharePoint

Top 10 Email Security Challenges and Solutions

Mastering Exchange 2007 Server Management – May 29, 2008 (11:00 AM EST)

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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing