跳到主要内容

如何批量删除Outlook中的所有空文件夹?

作者:凯莉 最后修改时间:2017-07-25

假设Outlook中的邮件文件夹下有数十个空文件夹,通常我们可以通过右键单击菜单来逐个删除这些空文件夹。 与反复单击右键相比,本文将介绍一种VBA,以快速批量删除一个Outlook文件夹的所有空子文件夹。

使用VBA批量删除Outlook中的所有空文件夹

Office 选项卡 - 在 Microsoft Office 中启用选项卡式编辑和浏览,让工作变得轻而易举
解锁 Outlook 的 Kutools 免费 版本 立即享受超过 70 项功能,永久无限制访问
使用这些高级功能增强您的 Outlook 2021 - 2010 或 Outlook 365。享受 70 多种强大功能并提升您的电子邮件体验!

箭头蓝色右气泡使用VBA批量删除Outlook中的所有空文件夹

要删除某个Outlook文件夹的所有空子文件夹,请执行以下操作:

1。 按 其他 + F11 键以打开“ Microsoft Visual Basic应用程序”窗口。

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文件夹的所有子文件夹。


箭头蓝色右气泡相关文章

在Outlook中按文件夹名称查找文件夹(完整的文件夹路径)


最佳办公生产力工具

最新消息:Kutools for Outlook 发布 免费版本!

体验全新的 Kutools for Outlook 免费版本拥有 70 多项令人难以置信的功能,您可以永久使用! 点击立即下载!

🤖 Kutools人工智能 : 具有人工智能魔力的即时专业电子邮件——一键天才回复、完美语气、多语言掌握。轻松改变电子邮件! ...

📧 电子邮件自动化: 自动回复(适用于 POP 和 IMAP)  /  安排发送电子邮件  /  发送电子邮件时按规则自动抄送/密件抄送  /  自动转发(高级规则)   /  自动添加问候语   /  自动将多收件人电子邮件拆分为单独的消息 ...

📨 电子邮件管理: 撤回电子邮件  /  按主题和其他人阻止诈骗电子邮件  /  删除重复的电子邮件  /  高级搜索  /  合并文件夹 ...

📁 附件专业版批量保存  /  批量分离  /  批量压缩  /  自动保存   /  自动分离  /  自动压缩 ...

🌟 界面魔法: 😊更多又漂亮又酷的表情符号   /  收到重要邮件时提醒您  /  最小化 Outlook 而不是关闭 ...

👍 一键奇迹: 使用传入附件回复全部  /   反网络钓鱼电子邮件  /  🕘显示发件人的时区 ...

👩🏼‍🤝‍👩🏻 通讯录和日历: 从选定的电子邮件中批量添加联系人  /  将联系人组拆分为各个组  /  删除生日提醒 ...

只需单击一下即可立即解锁 Kutools for Outlook -永久免费. 别等了, 立即下载并提高您的效率!

 

 

 

Comments (10)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
This worked great for me. Thank you. Some folders cannot be deleted as they are native to Outlook, but the sub-folders work great.
This comment was minimized by the moderator on the site
74 empty folders were deleted but unfortunately also 109 folders that were not. Other empty folders were left untouched.
This comment was minimized by the moderator on the site
Super easy and incredibly helpful. Thank you!!
This comment was minimized by the moderator on the site
I am getting the same error like Bryan.... and now?
This comment was minimized by the moderator on the site
The script tries to delete a folder that was already deleted.
I added a row after xFlag = False with this content:
on error resume next
This comment was minimized by the moderator on the site
I am getting the following error when run the above " Run-time error '-2147352567 (80020009)' Cannot delete this folder. Right-click the folder, and then click properties to check your permissions for the folder. See the folder owner or your administrator to change your permissions"

It appears the script moves 1 item to the deleted folder and then errors out.
This comment was minimized by the moderator on the site
Agree - I get the same error.
This comment was minimized by the moderator on the site
The script tries to delete a folder that was already deleted.
I added a row after xFlag = False with this content:
on error resume next
This comment was minimized by the moderator on the site
Indeed, add:

On Error Resume Next

AFTER:

Dim x Fldr As Folder 'Declare sub folder objects
xFlag = False

It should look like this:

Dim x Fldr As Folder 'Declare sub folder objects
xFlag = False
On Error Resume Next
This comment was minimized by the moderator on the site
Brilliant!!!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations