·
5 months ago
74 empty folders were deleted but unfortunately also 109 folders that were not. Other empty folders were left untouched.
假設Outlook中的郵件文件夾下有幾十個空文件夾,通常我們可以通過右鍵菜單逐個刪除空文件夾。 與重複右鍵單擊相比,本文將介紹VBA以批量快速刪除一個Outlook文件夾的所有空子文件夾。
要刪除某個Outlook文件夾的所有空子文件夾,請執行以下操作:
1。 按 其他 + F11 鍵以打開Microsoft Visual Basic for Applications窗口。
2。 點擊 插入 > 模塊,並將VBA代碼粘貼到新的模塊窗口中。
VBA:批量刪除某些Outlook文件夾的所有空子文件夾
Public Sub DeletindEmtpyFolder() Dim xFolders As Folders Dim xCount As Long Dim xFlag As Boolean Set xFolders = Application.GetNamespace("MAPI").PickFolder.Folders Do FolderPurge xFolders, xFlag, xCount Loop Until (Not xFlag) If xCount > 0 Then MsgBox "Deleted " & xCount & "(s) empty folders", vbExclamation + vbOKOnly, "Kutools for Outlook" Else MsgBox "No empty folders found", vbExclamation + vbOKOnly, "Kutools for Outlook" End If End Sub Public Sub FolderPurge(xFolders, xFlag, xCount) Dim I As Long Dim xFldr As Folder 'Declare sub folder objects xFlag = False If xFolders.Count > 0 Then For I = xFolders.Count To 1 Step -1 Set xFldr = xFolders.Item(I) If xFldr.Items.Count < 1 Then 'If the folder is empty check for subfolders If xFldr.Folders.Count < 1 Then 'If the folder contains not sub folders confirm deletion xFldr.Delete 'Delete the folder xFlag = True xCount = xCount + 1 Else 'Folder contains sub folders so confirm deletion FolderPurge xFldr.Folders, xFlag, xCount End If Else 'Folder contains items or (subfolders that may be empty). FolderPurge xFldr.Folders, xFlag, xCount End If Next End If End Sub
3。 按 F5 鍵或 跑 按鈕來運行這個VBA代碼。
4。 在彈出的選擇文件夾對話框中,請選擇您要批量刪除空子文件夾的特定文件夾,然後單擊 OK 按鈕。 看截圖:
5。 現在出現一個Kutools for Outlook對話框,並顯示已刪除多少個空子文件夾。 點擊 OK 按鈕關閉它。
到目前為止,指定的Outlook文件夾的所有子文件夾已被批量刪除。