Macro to delete all empty folders

  • Thread starter Dorian C. Chalom
  • Start date
D

Dorian C. Chalom

Does someone have a macro that will go through my inbox, nested folders and
all, and delete all empty folders?

Thank you
 
G

Graham Mayor

How deep are the inbox folders nested?
If you delete a folder with a nested folder the nested folder will also be
deleted. How would you want to handle that?
With only one level of nesting i.e.
Inbox > Folder > Subfolder
the following will check the sub-folders and provide the opportunity to
delete those that are empty of messages or sub sub-folders
It will then check the folders and provide the opportunity delete any that
are empty of messages, and have no subfolders.

Sub RemoveEmpties()
Dim i As Long, j As Long
Dim iFolder As Folder
Dim subFolder As Folder
Dim Ask As String
For i = Application.Session.GetDefaultFolder(olFolderInbox).Folders.Count To
1 Step -1
Set iFolder =
Application.Session.GetDefaultFolder(olFolderInbox).Folders(i)
For j = iFolder.Folders.Count To 1 Step -1
Set subFolder = iFolder.Folders(j)
If subFolder.Items.Count = 0 And _
subFolder.Folders.Count = 0 Then
Ask = MsgBox(iFolder.Name & " > " & subFolder.Name, vbYesNo,
"Delete sub folder")
If Ask = vbYes Then
subFolder.Delete
End If
End If
Next j
If iFolder.Items.Count = 0 And _
iFolder.Folders.Count = 0 Then
Ask = MsgBox(iFolder.Name, vbYesNo, "Delete folder")
If Ask = vbYes Then
iFolder.Delete
End If
End If
Next i
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top