In Outlook, I want to delete a folder if it has nothing in it. I don’t want to delete the built-in folders like Inbox, Outbox, Junk, etc., mostly because I get an error if I try. I couldn’t find a property of the MAPIFolder object that told me if it was a default folder. So I wrote this function:
Dim vaDefaults As Variant
Dim i As Long
Dim fldTest As MAPIFolder
vaDefaults = Array(olFolderCalendar, olFolderContacts, olFolderDeletedItems, _
olFolderDrafts, olFolderInbox, olFolderJournal, olFolderNotes, olFolderOutbox, _
olFolderSentMail, olFolderTasks, olFolderJunk)
For i = LBound(vaDefaults) To UBound(vaDefaults)
Set fldTest = Application.GetNamespace(“MAPI”).GetDefaultFolder(vaDefaults(i))
If fldTest.EntryID = fld.EntryID Then
FolderIsDefault = True
Exit Function
End If
Next i
FolderIsDefault = False
End Function
I loop through all of the constants in olDefaultFolders and compare the EntryIDs. If I get a match, it’s a default folder. I use it thusly:
If fldOld.Items.Count = 0 And fldOld.Folders.Count = 0 _
And Not FolderIsDefault(fldOld) Then
fldOld.Delete
End If
Check for items, check for subfolders, then check to see if it’s a default folder. I’m using it in this utility: More Outlook Tags. Sadly, it’s the best utility I’ve ever written.
Oh, I just realized I broke my own rule of starting all functions that return a Boolean with “Is”. I should have named it IsDefaultFolder.
An interesting approach, thanks. I wonder, though, whether it wouldn’t be easier to add the line “On Error Resume Next” at the top of the routine.
Stan
Easier? Yes. The problem is that I don’t know Outlook very well. The only reason I “realized” that I couldn’t delete a default folder was because I inadvertently tried and failed. I don’t want to mask all the other things I’m not supposed to be doing until I know what they are. :) But for production, On Error would be better because I *do* want to mask what I don’t know.
One way is to simply attempt to delete the folder, wrapping it in an “On Error Resume Next/On Error Goto 0? pair. If it’s a default folder, it won’t be deleted. Use “If Err = 0? to set the boolean flag to True.
You could also try renaming the folder (default folders can’t be renamed), if renaming causes an error, it’s a default folder and you can exit the function. Although if you can make other folders non-renameable (is that a word?), this won’t work to exclude non-default folders.
And finally you could write a routine to delete folders instead, simply ignoring errors that occur, and returns boolean True if the folder delete actually succeeded. A variation of my first suggestion, if you will.
To add to JP’s suggestion, I’d try to test for the specific error, e.g., if deleting an undeletable folder generates an error 1004 then say, “If Err.Number 1004?. This further lessens the possibility that some other error is being cloaked by the On Error Resume Next statment.
I forgot about the less than greater than brackets. There should be a “not equal” sign between “Err.Number” and “1004?. Let’s see if it works between the VB brackets.
You need to escape any HTML characters when posting comments. Hopefully this will print properly:
If Err.Number <> 1004
However it should have worked in the VBA code. :roll: