Purge Folders on Demand

Downloads
15990.zip

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.

Discuss this Article 8

John Smith (not verified)
on Mar 22, 2001
Great article. Where exactly is the GetMAPIFolder() function referred to in Listing 1?
Anonymous User (not verified)
on Jun 30, 2005
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 (not verified)
on May 4, 2005
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!
Sue Mosher (not verified)
on Mar 26, 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.

Please or Register to post comments.

IT/Dev Connections Exchange Server

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• Future Deopyments
and Integrations
• Hybrid Deployments
• Exchange Online
• Windows 8 Deployment
and much more!

Come See Tony Redmond & Mark Minasi in Person!

Early Registration Now Open

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.