如何将电子邮件从多个文件夹/子文件夹导出到Outlook中的Excel?
使用Outlook中的“导入和导出”向导导出文件夹时,它不支持 包括子文件夹 如果将文件夹导出到CSV文件,则为选项。 但是,将每个文件夹导出到CSV文件然后手动将其转换为Excel工作簿将非常耗时且乏味。 在这里,本文将介绍一个VBA,可以轻松地将多个文件夹和子文件夹快速导出到Excel工作簿。
Kutools for Outlook-为Microsoft Outlook带来100种强大的高级功能
- 自动CC / BCC 根据规则发送电子邮件; 自动转发 按规则发送多封电子邮件; 自动回复 没有交换服务器,还有更多自动功能...
- BCC警告 -如果您的邮件地址在密件抄送列表中,则当您尝试全部答复时显示消息; 缺少附件时提醒,还有更多提醒功能...
- 回复(全部)带有所有附件 在邮件对话中; 一次回复许多电子邮件; 自动添加问候语 回复时自动将日期和时间添加到主题中...
- 附件工具:自动分离,全部压缩,重命名,自动保存所有... 快速报告,计算所选邮件, 删除重复的邮件和联系人...
- 超过100种高级功能将 解决您的大部分问题 在 Outlook 2021 - 2010 或 Office 365 中。完整功能 60 天免费试用。
使用VBA将多个文件夹中的多个电子邮件导出到Excel
请按照以下步骤使用Outlook中的VBA将电子邮件从多个文件夹或子文件夹导出到Excel工作簿。
1。 按 其他 + F11 键以打开“ Microsoft Visual Basic应用程序”窗口。
2。 点击 插页 > 模块,然后将以下VBA代码粘贴到新的“模块”窗口中。
VBA:将电子邮件从多个文件夹和子文件夹导出到Excel
Const MACRO_NAME = "Export Outlook Folders to Excel"
Sub ExportMain()
ExportToExcel "destination_folder_path\A.xlsx", "your_email_accouny\folder\subfolder_1"
ExportToExcel "destination_folder_path\B.xlsx", "your_email_accouny\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
'Write Excel Column Headers
With excWks
.Cells(1, 1) = "Subject"
.Cells(1, 2) = "Received"
.Cells(1, 3) = "Sender"
End With
intRow = 2
For Each olkMsg In olkFld.Items
'Only export messages, not receipts or appointment requests, etc.
If olkMsg.Class = olMail Then
'Add a row for each field in the message you want to export
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 olkPrp = Nothing
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)更换 目的地_文件夹_路径 在上面的代码以及目标文件夹的文件夹路径中,您将保存导出的工作簿,例如 C:\ Users \ DT168 \ Documents \ TEST.
(2)将上述代码中的your_email_accouny \ folder \ subfolder_1和your_email_accouny \ folder \ subfolder_2替换为Outlook中子文件夹的文件夹路径,例如 凯莉@extendoffice.com \ Inbox \ A 和 凯莉@extendoffice.com \ Inbox \ B
4。 按 F5 键或单击 运行 按钮以运行此VBA。 然后点击 OK 弹出Outlook文件夹导出到Excel对话框中的按钮。 看截图:
现在,来自以上VBA代码中所有指定子文件夹或文件夹的电子邮件将被导出并保存到Excel工作簿中。
相关文章
Kutools for Outlook-为Outlook带来100个高级功能,并使工作更加轻松!
- 自动CC / BCC 根据规则发送电子邮件; 自动转发 自定义多封电子邮件; 自动回复 没有交换服务器,还有更多自动功能...
- BCC警告 -当您尝试全部答复时显示消息 如果您的邮件地址在“密件抄送”列表中; 缺少附件时提醒,还有更多提醒功能...
- 在邮件对话中回复(全部)带有所有附件; 回复许多电子邮件 很快; 自动添加问候语 回复时将日期添加到主题中...
- 附件工具:管理所有邮件中的所有附件, 自动分离, 全部压缩,全部重命名,全部保存...快速报告, 计算选定的邮件...
- 强大的垃圾邮件 习俗 删除重复的邮件和联系人... 使您能够在Outlook中做得更聪明,更快和更好。

