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


December 2000

Purge Folders on Demand


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

Download the Code Here

Outlook VBA on Demand

Gordon Filby, a reader from Germany, wants to build an Outlook VBA macro that deletes all the items from several folders. He says his list of folders won't change often. However, I want to be able to modify the list of purged folders without needing to update the code. My solution is to use a Microsoft Outlook folder to hold the folder list. This Outlook folder will contain one item for each purged folder.

First, create a folder to hold Mail items. This folder can reside in an Exchange Server Public Folders hierarchy or in your personal mailbox or folders. The sample code in Listing 1 uses a folder named Folders to Purge in a Personal Folders file named VBA Data. You need to provide the path to the folder in the section of Listing 1 marked #### User Options ####.

In this folder, create a post for each folder that you want to purge. The subject of the post must be the full path to the folder (e.g., "Public Folders/All Public Folders/My Folders/My List").

Listing 1's CleanFolders subroutine uses the GetMAPIFolder() function, which you've seen in previous Outlook VBA on Demand columns. This function takes as its argument the full path to an Outlook folder and returns a MAPIFolder object. (Because this function is basic to Outlook programming, I haven't included it here. You can obtain the function, along with the other procedures, from my past columns on the Windows 2000 Magazine Web site.) Using the value set in the code for the strFoldersPurged variable, the CleanFolders procedure first employs the GetMAPIFolder() function to obtain the folder that holds the names of the folders you want to purge. Then, using the path set in the item's Subject property, the procedure loops through the items in that folder and uses the GetMAPIFolder() function again to obtain each folder you want to purge. If you don't provide a valid path, the CleanFolders routine simply ignores that folder.

After CleanFolders obtains a folder to be purged, the subroutine passes the folder to the PurgeFolder() function, which you see in Listing 2. The heart of the PurgeFolder() function is a Do loop that uses the GetFirst, GetNext, and Delete methods to delete each item in the folder and keep a running count. The PurgeFolder() function returns a string, which CleanFolders uses to build a message box that reports to the user how many items the program removed from which folders.

Notice the syntax of the CleanFolders procedure's MsgBox statements. Prompt and Title are named arguments. When a method includes named arguments, you can make your code more readable by using the arg:= syntax to label each argument. This capability is particularly helpful when you're working with complex strings.

End of Article



Reader Comments
Great article. Where exactly is the GetMAPIFolder() function referred to in Listing 1?

John Smith March 22, 2001


Unfortunately, the entire code module did not get posted with the article. A VBScript version of the GetMAPIFolder() function is included with a form I've posted at http://www.slipstick.com/dev/olforms/saveinfolder.htm. Or you can substitute the function from http://www.slipstick.com/dev/code/getfolder.htm.

Sue Mosher March 26, 2001


Anyone have an idea on how to modify this so it only deletes items that are x days old?

That would make this the perfect solution for me. Thanks!

Anonymous User May 04, 2005 (Article Rating: )


I guess I am rather ignorant of the folder names used in Outlook. If I have a folder called 'FoldersToPurge' in the root of my local folders to do I assign strDataFolder = "FoldersToPurge"?

I SO WANT TO GET THIS WORKING

Andy Godfrey

Anonymous User June 30, 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
WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

Command Prompt Tricks

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

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Development Whitepapers Global Trends: Unified SOA Performance Management Matters

The role of Service Level Agreements in Successful SOA Deployments

Related Events The Secrets to Successfully Migrating Exchange Public Folders to SharePoint

Deep Dive into Windows Server 2008 R2 presented by John Savill

Oracle Developer Day Online - EUROPE

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.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement