跳到主要内容

启动Outlook时如何自动展开所有文件夹?

作者:凯莉 最后修改时间:2023-12-12

通常,当您在 Outlook 导航窗格中展开或折叠文件夹并随后关闭程序时,该文件夹在重新打开 Outlook 时将保留其展开或折叠状态。 但是,如果您希望每次启动 Outlook 时自动展开所有文件夹该怎么办? 本文提供了一个 VBA 解决方案来实现这一目标。

使用 VBA 启动 Outlook 时自动展开所有文件夹

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

箭头蓝色右气泡 使用 VBA 启动 Outlook 时自动展开所有文件夹

本指南将演示如何使用 VBA 脚本在每次启动 Outlook 时自动展开导航窗格中的所有文件夹。 请按照以下步骤实施 VBA 脚本:

  1. 媒体 Alt + F11键 同时打开 Microsoft Visual Basic应用程序 窗口。
  2. 展开 1项目Microsoft Outlook对象 ,在 项目 窗格,双击打开 本次展望会议 窗口,然后将以下 VBA 代码粘贴到其中。

    VBA:自动展开Outlook中的所有文件夹

    Public WithEvents GEx As Explorer
    Public GFlag As Boolean
    Private Sub Application_Startup()
      'Update by ExtendOffice 2023/12/08
      Set GEx = Application.ActiveExplorer
      GFlag = False
    End Sub
    
    Private Sub GEx_SelectionChange()
      If GFlag = False Then
        ExpandAllFolders
      End If
      GFlag = True
    End Sub
    
    Public Sub ExpandAllFolders()
      Dim xNameSpace As Outlook.NameSpace
      Dim xFlds As Outlook.Folders
      Dim xCurrFld As Outlook.MAPIFolder
      Dim xFld As Outlook.MAPIFolder
      Dim xExpandDefaultStoreOnly As Boolean
      Dim xModule As NavigationModule
      On Error Resume Next
      xExpandDefaultStoreOnly = False
      Set xNameSpace = Application.Session
      Set xModule = Application.ActiveExplorer.NavigationPane.CurrentModule
      Set xCurrFld = Application.ActiveExplorer.CurrentFolder
      If xExpandDefaultStoreOnly = True Then
        Set xFld = xNameSpace.GetDefaultFolder(olFolderInbox)
        Set xFld = xFld.Parent
        Set xFlds = xFld.Folders
        LoopFolders xFlds, True
      Else
        LoopFolders xNameSpace.Folders, True
        LoopFolders xNameSpace.Folders, False
      End If
      DoEvents
      Set Application.ActiveExplorer.NavigationPane.CurrentModule = xModule
      Set Application.ActiveExplorer.CurrentFolder = xCurrFld
      Set xNameSpace = Nothing
      Set xModule = Nothing
      Set xCurrFld = Nothing
    End Sub
    
    Private Sub LoopFolders(Flds As Outlook.Folders, ByVal All As Boolean)
      Dim xFld As Outlook.MAPIFolder
      On Error Resume Next
      For Each xFld In Flds
        Select Case All
          Case True
            If xFld.DefaultItemType = olMailItem Then
              Set Application.ActiveExplorer.CurrentFolder = xFld
              DoEvents
              If xFld.Folders.Count > 0 Then
                LoopFolders xFld.Folders, All
              End If
            End If
          Case False
            Set Application.ActiveExplorer.CurrentFolder = xFld
            DoEvents
            If xFld.Folders.Count > 0 Then
              LoopFolders xFld.Folders, All
            End If
        End Select
      Next
    End Sub
  3. 保存代码并关闭 Microsoft Visual Basic应用程序 窗口。

从现在开始,重新启动Outlook时,所有文件夹将在Outlook中自动展开。

笔记:

  • 重新启动 Outlook 时,此 VBA 无法打开折叠的搜索文件夹。
  • 为了确保 VBA 脚本正常运行,必须调整 Outlook 中的宏设置。 请前往 文件 > 附加选项 > 信任中心 > 信任中心设置 > 宏设置,并选择 启用所有宏 选项。 此设置允许 VBA 脚本不受任何限制地运行。

箭头蓝色右气泡 相关文章


最佳办公生产力工具

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

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

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

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

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

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

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

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

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

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

 

 

 

Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Just tried this on Microsoft® Outlook® for Microsoft 365 MSO (Version 2310 Build 16.0.16924.20054) 64-bit and it was unsuccessful
This comment was minimized by the moderator on the site
Hi there,

Please go to File > Options > Trust Center > Trust Center Settings > Macro Settings. And select the Enable all macros option. After making this change, restart Outlook, the macro should then be operational.
This comment was minimized by the moderator on the site
Hi guys,

does this code also work for Microsoft outlook 365?
I have tried and tried again, but I get no results.
Thanks.
This comment was minimized by the moderator on the site
I couldn't get this macro to work either.
This comment was minimized by the moderator on the site
Hi there,
We've updated the code, please try it again. 🙂
Amanda
This comment was minimized by the moderator on the site
I cannot get this macro to work at all.  All folders in the Folder pane are not expanded at all.  I am using Outlook 2021.  Is there some instruction I missed?
This comment was minimized by the moderator on the site
Hi there,
We've updated the code, please try it again. 🙂
Amanda
This comment was minimized by the moderator on the site
Thank you so much.. It solved my problem. :)
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations