Thursday, January 7, 2010

Outlook macro - Empty selected folder

I wrote this short little macro to have the ability to empty out any folder that is selected of mail items, just like the deleted items folder. Works on search folders as well. I created a button for it on my toolbar to make it easily runnable.

Sub EmptySelectedFolder()
Dim objFolder As Outlook.MAPIFolder

Set objFolder = Application.ActiveExplorer.CurrentFolder

If objFolder Is Nothing Then
MsgBox "No folder selected.", vbCritical
Exit Sub
End If

'make no the default button in case they skip reading my warning, and hit enter or space
If MsgBox("Are you sure you want to empty out this folder, and permanently delete all the mail?", vbYesNo Or vbDefaultButton2 Or vbQuestion) = vbYes Then
For i = objFolder.Items.Count To 1 Step -1
If (TypeOf objFolder.Items.Item(1) Is Outlook.MailItem) Then
objFolder.Items.Remove i 'using .remove as it deletes perm

If (i Mod 100) = 0 Then
End If
End If
End If
End Sub