Skip to main content

如何將多個資料夾/子資料夾中的郵件匯出到Outlook的Excel中?

Author: Kelly Last Modified: 2025-05-12

如果依靠手動方法或有限的Outlook功能,從多個資料夾或子資料夾匯出郵件到Excel可能會是一項繁瑣的任務。幸運的是,有更有效的方法來實現這一點。在本文中,我們將探討兩種方法:基於VBA的方法和使用Kutools for Outlook更快、更用戶友好的解決方案。雖然VBA方法提供了靈活性,但需要複雜的設置和調整。相比之下,Kutools for Outlook提供了一種簡單而強大的方式,只需點擊幾下即可將郵件匯出到Excel,節省您的時間和精力。讓我們深入瞭解詳細信息。

使用VBA將郵件從多個資料夾/子資料夾匯出到Excel 複雜但靈活

使用Kutools for Outlook將郵件從資料夾/子資料夾匯出到Excel ?高效且易用


使用VBA將郵件從多個資料夾/子資料夾匯出到Excel

如果您熟悉編碼並且需要可自定義的解決方案,使用VBA可能非常有效。此方法允許您指定多個資料夾或子資料夾並將其郵件匯出到單獨的Excel文件中。然而,它需要技術知識並仔細定制代碼。

步驟1:打開VBA編輯器

按下 Alt + F11 啟動“Microsoft Visual Basic for Applications”窗口。

步驟2:插入新模塊

點擊“插入”>“模塊”,然後將以下VBA代碼粘貼到新的模塊窗口中。

VBA:將郵件從多個資料夾和子資料夾匯出到Excel

Const MACRO_NAME = "Export Outlook Folders to Excel"
Sub ExportMain()
ExportToExcel "destination_folder_path\A.xlsx", "your_email_account\folder\subfolder_1"
ExportToExcel "destination_folder_path\B.xlsx", "your_email_account\folder\subfolder_2"
MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportToExcel(strFilename As String, strFolderPath As String)
Dim olkMsg As Object
Dim olkFld As Object
Dim excApp As Object
Dim excWkb As Object
Dim excWks As Object
Dim intRow As Integer
Dim intVersion As Integer

If strFilename <> "" Then
If strFolderPath <> "" Then
Set olkFld = OpenOutlookFolder(strFolderPath)
If TypeName(olkFld) <> "Nothing" Then
intVersion = GetOutlookVersion()
Set excApp = CreateObject("Excel.Application")
Set excWkb = excApp.Workbooks.Add()
Set excWks = excWkb.ActiveSheet
With excWks
.Cells(1, 1) = "Subject"
.Cells(1, 2) = "Received"
.Cells(1, 3) = "Sender"
End With
intRow = 2
For Each olkMsg In olkFld.Items
If olkMsg.Class = olMail Then
excWks.Cells(intRow, 1) = olkMsg.Subject
excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
intRow = intRow + 1
End If
Next
Set olkMsg = Nothing
excWkb.SaveAs strFilename
excWkb.Close
Else
MsgBox "The folder '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
End If
Else
MsgBox "The folder path was empty.", vbCritical + vbOKOnly, MACRO_NAME
End If
Else
MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
End If

Set olkMsg = Nothing
Set olkFld = Nothing
Set excWks = Nothing
Set excWkb = Nothing
Set excApp = Nothing
End Sub

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
Dim arrFolders As Variant
Dim varFolder As Variant
Dim bolBeyondRoot As Boolean

On Error Resume Next
If strFolderPath = "" Then
Set OpenOutlookFolder = Nothing
Else
Do While Left(strFolderPath, 1) = "\"
strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
Loop
arrFolders = Split(strFolderPath, "\")
For Each varFolder In arrFolders
Select Case bolBeyondRoot
Case False
Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
bolBeyondRoot = True
Case True
Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
End Select
If Err.Number <> 0 Then
Set OpenOutlookFolder = Nothing
Exit For
End If
Next
End If
On Error GoTo 0
End Function

Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
Dim olkSnd As Outlook.AddressEntry
Dim olkEnt As Object

On Error Resume Next
Select Case intOutlookVersion
Case Is < 14
If Item.SenderEmailType = "EX" Then
GetSMTPAddress = SMTPEX(Item)
Else
GetSMTPAddress = Item.SenderEmailAddress
End If
Case Else
Set olkSnd = Item.Sender
If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
Set olkEnt = olkSnd.GetExchangeUser
GetSMTPAddress = olkEnt.PrimarySmtpAddress
Else
GetSMTPAddress = Item.SenderEmailAddress
End If
End Select
On Error GoTo 0
Set olkSnd = Nothing
Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
Dim arrVer As Variant
arrVer = Split(Outlook.Version, ".")
GetOutlookVersion = arrVer(0)
End Function

Function SMTPEX(olkMsg As Outlook.MailItem) As String
Dim olkPA As Outlook.propertyAccessor
On Error Resume Next
Set olkPA = olkMsg.propertyAccessor
SMTPEX = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
On Error GoTo 0
Set olkPA = Nothing
End Function

步驟3:自定義VBA代碼

  1. 將上述代碼中的“destination_folder_path”替換為實際的資料夾路徑,例如“C:\Users\DT168\Documents\TEST”。
  2. 將“your_email_account\folder\subfolder_1”和“your_email_account\folder\subfolder_2”替換為您的Outlook資料夾路徑,例如“Kelly@extendoffice.com\收件匣\A”和“Kelly@extendoffice.com\收件匣\B”。
    doc-export-subfolders-to-excel-1

步驟4:執行代碼

按下“F5”或點擊“執行”按鈕以執行代碼。然後在彈出的“將Outlook資料夾匯出到Excel”對話框中點擊“確定”按鈕。現在所有指定資料夾/子資料夾中的郵件都已匯出到Excel工作簿中。
doc-export-subfolders-to-excel-3

缺點:

  • 複雜的設置:需要仔細調整代碼並具備VBA的工作知識。
  • 容易出錯:在自定義過程中出現的小錯誤可能會導致錯誤或不完整的匯出。
  • 耗時:不適合快速或頻繁的匯出。

通過批量處理轉變您的郵件管理!

厭倦了重複的郵件任務嗎? Kutools for Outlook 提供“批量處理”工具來簡化您的工作流程並節省寶貴的時間。

  • ? 批量回覆郵件:使用模板輕鬆進行批量回覆。
  • ? 單獨轉寄:將多封郵件作為普通消息轉寄,而不是作為附件。
  • ? 保存為多種格式:一次性將郵件導出為PDF、Word、Excel等格式!
Kutools for Outlook Bulk Processing Feature

立即試用 Kutools for Outlook


使用 Kutools for Outlook 將郵件從資料夾/子資料夾匯出到 Excel ?

對於更快捷且無煩惱的方法,“Kutools for Outlook”提供了“將選定郵件保存為多種格式文件”的功能。由於其簡潔性、速度和多功能性,這種方法非常推薦。無論您是管理少量郵件還是大量數據集,Kutools 都能確保輕鬆無壓力的體驗。

使用 Kutools for Outlook 解鎖極致郵件效率!永久免費獲取 70 項強大功能。立即下載免費版本

步驟1:選擇資料夾或子資料夾

導航到包含要匯出郵件的資料夾或子資料夾。按下 Ctrl + A 選擇列表中的所有郵件。

步驟2:訪問“將選定郵件保存為多種格式文件”功能

點擊“Kutools”>“批量處理”>“將選定郵件保存為多種格式文件”。

doc-export-subfolders-to-excel-4

步驟3:配置匯出設置

  1. 在“保存郵件為其他文件”對話框中,選擇要保存文件的目標資料夾。
  2. 選擇“Excel 格式”選項。
  3. 在“保存內容”部分選擇要匯出的特定郵件內容(例如,郵件頭、正文)。
    doc-export-subfolders-to-excel-5

步驟4:完成匯出

點擊“確定”開始匯出過程。完成後,您會在指定資料夾中找到所有保存為單獨Excel文件的郵件。

doc-export-subfolders-to-excel-6

優勢:

  • 快速直觀:只需點擊幾下即可將郵件匯出到Excel——不需要技術技能。
  • 可自定義輸出:選擇特定的文件格式和內容以滿足您的需求。
  • 無錯誤過程:避免與VBA方法相關的複雜性和潛在錯誤。
  • 專業結果:非常適合創建有組織的記錄或高效共享數據。
注意: 要應用Kutools for Outlook的“將選定郵件保存為多種格式文件”實用程序,首先,您應該下載並安裝Kutools for Outlook