跳到主要內容

啟動Outlook時如何自動展開所有文件夾?

通常,當您在 Outlook 導覽窗格中展開或折疊資料夾並隨後關閉程式時,該資料夾會在重新開啟 Outlook 時保留其展開或折疊狀態。 但是,如果您希望每次啟動 Outlook 時自動展開所有資料夾該怎麼辦? 本文提供了一個 VBA 解決方案來實現這一目標。

使用 VBA 啟動 Outlook 時自動展開所有資料夾

Office 標籤 - 在 Microsoft Office 中啟用選項卡式編輯和瀏覽,讓工作變得輕而易舉
Kutools for Outlook - 透過 100 多個進階功能增強 Outlook,實現卓越效率
使用這些進階功能增強您的 Outlook 2021 - 2010 或 Outlook 365。 享受全面的 60 天免費試用並提升您的電子郵件體驗!

箭頭藍色右氣泡 使用 VBA 啟動 Outlook 時自動展開所有資料夾

本指南將示範如何使用 VBA 腳本在每次啟動 Outlook 時自動展開導覽窗格中的所有資料夾。 請依照以下步驟實作 VBA 腳本:

  1. 媒體推薦 Alt + F11鍵 同時打開 Microsoft Visual Basic for Applications 窗口。
  2. 展開 項目1Microsoft 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 for Applications 窗口。

從現在開始,重新啟動Outlook時,所有文件夾將在Outlook中自動展開。

筆記:

  • 重新啟動 Outlook 時,此 VBA 無法開啟已折疊的搜尋資料夾。
  • 為了確保 VBA 腳本正常運行,必須調整 Outlook 中的巨集設定。 請前往 文件 > 選項 > 信託中心 > 信任中心設置 > 宏設置,並選擇 啟用所有的宏 選項。 此設定允許 VBA 腳本不受任何限制地運行。

箭頭藍色右氣泡 相關文章


最佳辦公生產力工具

Kutools for Outlook - 超過 100 種強大的功能可增強您的 Outlook

🤖 人工智慧郵件助手: 具備人工智慧魔力的即時專業電子郵件——一鍵天才回覆、完美語調、多語言掌握。輕鬆改變電子郵件! ……

📧 電子郵件自動化: 外出(適用於 POP 和 IMAP)  /  安排發送電子郵件  /  發送電子郵件時按規則自動抄送/密件副本  /  自動轉送(進階規則)   /  自動添加問候語   /  自動將多收件者電子郵件拆分為單獨的訊息 ...

📨 電子郵件管理: 輕鬆回憶電子郵件  /  按主題和其他人阻止詐騙電子郵件  /  刪除重複的電子郵件  /  進階搜索  /  合併資料夾 ...

📁 附件專業版批量保存  /  批量分離  /  批量壓縮  /  自動保存   /  自動分離  /  自動壓縮 ...

🌟 介面魔法: 😊更多又漂亮又酷的表情符號   /  使用選項卡式視圖提高 Outlook 工作效率  /  最小化 Outlook 而不是關閉 ...

👍 一鍵奇蹟: 使用傳入附件回覆全部  /   反網路釣魚電子郵件  /  🕘顯示寄件者的時區 ...

👩🏼‍🤝‍👩🏻 通訊錄和行事曆: 從選定的電子郵件中大量新增聯絡人  /  將聯絡人群組拆分為各組  /  刪除生日提醒 ...

超過 100特點 等待您的探索! 按此處了解更多。

閱讀更多       免費下載      購買
 

 

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