In the December 2000 Outlook VBA on Demand, I demonstrated how to set up a system for purging specific folders on demand. This time, I dig deeper into the notion of deleting items from folders. You'll learn how to remove appointment items, including recurring appointments, and how to purge only items that occurred before a specific date.
First, you need to choose a date property on which to build the filter. The logical property to use in a calendar folder is End. Suppose a calendar folder includes an event that begins February 25, 2001, and ends March 4, 2001. You want to purge all items older than March 1. If you use the Start date as the property for your filter criteria, the purged items will include the 8-day event because its Start date occurs before March 1. However, if you use the End property, the item will remain on the calendar because the End date is more recent than your March 1 cutoff date.
The PurgeCalendarFolder() function, which you see in Listing 1, takes a MAPIFolder object as its single argument and returns a string that reports the number of items the function removed. You'll need to call PurgeCalendarFolder() from another procedure, such as the December column's CleanFolders subroutine. For testing, you can obtain a TestPurgeCalendarFolder() subroutine from the Windows 2000 Magazine Web site. (Go to http://www.win2000mag.com, enter 16207 in the InstantDoc ID text box, and go to the Article Information box to download the 16207.zip file.) This subroutine lets you pick a folder from the Select Folder dialog box. Notice that the PurgeCalendarFolder() function uses the DefaultItemType property to check the type of folder, then proceeds only if the folder is a calendar folder (i.e., a folder containing appointment items).
The two statements below cause the collection of items in the folder (i.e., colItems) to treat individual recurrences as separate appointments. Therefore, you can purge older instances of recurring appointments while leaving more recent instances in the folder. The IncludeRecurrences property is effective only if you use the Start property to sort the collection, as follows:
colItems.Sort "\[Start\]" colItems.IncludeRecurrences _ = True
The GetRestrictDate() function, (Web Listing 1) which you can obtain from the Windows 2000 Magazine Web site, displays an input box in which the user can type a date. Alternatively, to instruct Microsoft Outlook to delete all items in the folder, the user can leave the box blank. If the user enters information other than a date, the function calls itself until the user provides valid input. When you use such recursive functions, be sure to provide a method for the user to escape the loop—in this case, by leaving the box blank or providing a valid date.
The PurgeCalendarFolder() function uses the date that GetRestrictDate() returns to build a restriction on the colItems collection. For example, if a user enters "9/30/2000", the function builds the following string, which the Restrict method will use:
\[End\] < "Sep 30, 2000"
This example highlights an Outlook syntax quirk: The Restrict and Find methods won't work with a date literal, such as #9/30/2000#. Instead, they need a string.
After you apply the restriction to the colItems collection to get a new collection, colOldItems (containing only the older items), the PurgeCalendarFolder() function operates essentially the same way that the PurgeFolder() function in the December column operates—it deletes all the items in the collection.