Macro to delete all empty folders

  • Follow


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

Thank you 


0
Reply Dorian 6/4/2010 3:51:33 PM

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
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


"Dorian C. Chalom" <DChalom@Comcast.net> wrote in message 
news:OVLpT3$ALHA.348@TK2MSFTNGP06.phx.gbl...
> Does someone have a macro that will go through my inbox, nested folders 
> and all, and delete all empty folders?
>
> Thank you
> 


0
Reply gmayor (48) 6/7/2010 8:53:33 AM


1 Replies
866 Views

(page loaded in 0.048 seconds)

8/18/2012 8:28:29 PM


Reply: