Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

November 20, 2000 12:00 AM

Purge Folders on Demand

Windows IT Pro
InstantDoc ID #15990
Rating: (2)
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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    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
    7 years ago
    May 04, 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
    11 years ago
    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.

  • John Smith
    11 years ago
    Mar 22, 2001

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

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.