انتقل إلى المحتوى الرئيسي

كيفية تصدير رسائل البريد الإلكتروني من عدة مجلدات / مجلدات فرعية للتميز في Outlook؟

عند تصدير مجلد باستخدام معالج الاستيراد والتصدير في Outlook ، فإنه لا يدعم ملف وتشمل المجلدات الفرعية الخيار إذا قمت بتصدير المجلد إلى ملف CSV. ومع ذلك ، سيكون تصدير كل مجلد إلى ملف CSV ثم تحويله إلى مصنف Excel يدويًا مضيعة للوقت ومملة. هنا ، ستقدم هذه المقالة VBA لتصدير مجلدات ومجلدات فرعية متعددة بسرعة إلى مصنفات Excel بسهولة.

تصدير رسائل بريد إلكتروني متعددة من مجلدات / مجلدات فرعية متعددة إلى Excel باستخدام VBA

علامة تبويب Office - تمكين التحرير والتصفح المبوب في Microsoft Office، مما يجعل العمل سهلاً
Kutools for Outlook - عزز Outlook بأكثر من 100 ميزة متقدمة لتحقيق كفاءة فائقة
عزز Outlook 2021 - 2010 أو Outlook 365 الخاص بك باستخدام هذه الميزات المتقدمة. استمتع بتجربة مجانية شاملة مدتها 60 يومًا وارفع مستوى تجربة بريدك الإلكتروني!

السهم الأزرق الحق فقاعة تصدير رسائل بريد إلكتروني متعددة من مجلدات / مجلدات فرعية متعددة إلى Excel باستخدام VBA

يرجى اتباع الخطوات التالية لتصدير رسائل البريد الإلكتروني من عدة مجلدات أو مجلدات فرعية إلى مصنفات Excel باستخدام VBA في Outlook.

1. صحافة قديم + F11 مفاتيح لفتح نافذة ميكروسوفت فيسوال باسيك للتطبيقات.

2. انقر إدراج > وحدة، ثم قم بلصق كود فبا أدناه في نافذة الوحدة النمطية الجديدة.

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. يرجى ضبط رمز فبا أعلاه حسب حاجتك.

(1) استبدل Destination_folder_path في الكود أعلاه مع مسار المجلد لمجلد الوجهة ، ستحفظ المصنفات التي تم تصديرها ، مثل C: \ Users \ DT168 \ Documents \ TEST.
(2) استبدل your_email_accouny \ folder \ subfolder_1 و your_email_accouny \ folder \ subfolder_2 في الكود أعلاه بمسارات مجلدات المجلدات الفرعية في Outlook ، مثل \ علبة الوارد \ أ و \ صندوق الوارد \ ب

4. اضغط على F5 مفتاح أو انقر فوق يجري زر لتشغيل هذا VBA. ثم انقر فوق ملف OK زر في مربع الحوار تصدير مجلدات Outlook إلى Excel المنبثقة. انظر لقطة الشاشة:

والآن يتم تصدير رسائل البريد الإلكتروني من جميع المجلدات الفرعية أو المجلدات المحددة في رمز VBA أعلاه وحفظها في مصنفات Excel.


السهم الأزرق الحق فقاعةمقالات ذات صلة


أفضل أدوات إنتاجية المكتب

كوتولس لتوقعات - أكثر من 100 ميزة قوية لتعزيز توقعاتك

🤖 مساعد بريد الذكاء الاصطناعي: رسائل بريد إلكتروني احترافية فورية مع سحر الذكاء الاصطناعي - بنقرة واحدة للردود العبقرية، والنغمة المثالية، وإتقان متعدد اللغات. تحويل البريد الإلكتروني دون عناء! ...

📧 أتمتة البريد الإلكتروني: خارج المكتب (متوفر لـ POP وIMAP)  /  جدولة إرسال رسائل البريد الإلكتروني  /  نسخة تلقائية/نسخة مخفية الوجهة حسب القواعد عند إرسال البريد الإلكتروني  /  إعادة التوجيه التلقائي (القواعد المتقدمة)   /  إضافة تحية تلقائية   /  تقسيم رسائل البريد الإلكتروني متعددة المستلمين تلقائيًا إلى رسائل فردية 

📨 إدارة البريد الإلكتروني: استدعاء رسائل البريد الإلكتروني بسهولة  /  حظر رسائل البريد الإلكتروني الاحتيالية حسب الموضوعات والآخرين  /  حذف رسائل البريد الإلكتروني المكررة  /  المزيد من خيارات البحث  /  توحيد المجلدات 

📁 المرفقات بروحفظ دفعة  /  فصل دفعة  /  ضغط دفعة  /  حفظ تلقائي   /  فصل تلقائي  /  ضغط تلقائي 

؟؟؟؟ واجهة ماجيك: 😊 المزيد من الرموز التعبيرية الجميلة والرائعة   /  عزز إنتاجية Outlook الخاص بك باستخدام طرق العرض المبوبة  /  تصغير Outlook بدلاً من الإغلاق 

؟؟؟؟ بنقرة واحدة عجائب: الرد على الكل بالمرفقات الواردة  /   رسائل البريد الإلكتروني لمكافحة التصيد  /  🕘إظهار المنطقة الزمنية للمرسل 

👩🏼‍🤝‍👩🏻 جهات الاتصال والتقويم: دفعة إضافة جهات الاتصال من رسائل البريد الإلكتروني المحددة  /  تقسيم مجموعة اتصال إلى مجموعات فردية  /  إزالة تذكير عيد ميلاد 

على مدى ميزات 100 في انتظار الاستكشاف الخاص بك! انقر هنا لاكتشاف المزيد.

 

 

Comments (10)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I run this macro but keep getting compile error:

User=defined type not defined

On line 62 " Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder "

I have already specified the path as follows:

ExportToExcel "C:\Users\kudus\Documents\MailExportTest\f1\A.xlsx", "\Inbox\Black Hat Webcast"
ExportToExcel "C:\Users\\Documekudus\Documents\MailExportTest\f2\B.xlsx", "\Inbox\CPD\Kaplan Training"

I'm using Outlook 2016 in case that's needed
This comment was minimized by the moderator on the site
I fixed it. From the visual basic window, go to Tools Reference - and the box for "Microsoft Outlook 16.0 Object Library"

This comment was minimized by the moderator on the site
Hi,
I just ran this Macro which works fine.
I understand that in the expressions
excWks.Cells(intRow, 1) = olkMsg.Subject
excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)

the olkMsg.* and GetSMTPAddress(olkMsg, intVersion) extract stuff from Outlook.

What is the argument to use to get the Address the mail was sent to?

When Using the Export Wizard of Outlook, it is possible to export this address, so I assume it would be possible to do it through this Macro (with some modification).
Can somebody help?

Regards
This comment was minimized by the moderator on the site
Hi, Hopefully someone can help me out here, I have virtually no knowledge of VB but have managed to get this script working for me so far.

However I have around 1500 folders and subfolders under my inbox in total and I would really like a simple script to export all of the email address that I have sent to with the subject line and date on separate columns in Excel.

I have searched for days, and tried many different sites but cannot get any code to work other than this one.


Is what I am asking for even possible? If so is there anyone out there kind and clever enough to help me out whit the script I need?
I presume it has something to do with this part:


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


Thanks in advanced
This comment was minimized by the moderator on the site
hello dear, every thing working well many thanks but the body is not exported, how can i export email body too, the excel file has just (Subject, Received, and Sender), if you can update me with it will solve a huge matter in my business many thanks again
This comment was minimized by the moderator on the site
In the ExporttoExcel sub you can add the body

'Write Excel Column Headers
With excWks
.Cells(1, 1) = "Subject"
.Cells(1, 2) = "Received"
.Cells(1, 3) = "Sender"
.Cells(1, 4) = "Body"
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)
excWks.Cells(intRow, 4) = olkMsg.Body
intRow = intRow + 1
This comment was minimized by the moderator on the site
Hi Montaser,
The VBA script runs based on Outlook’s Export feature which doesn’t support exporting message content when bulk exporting emails from a mail folder. Therefore, this VBA script cannot export message content too.
This comment was minimized by the moderator on the site
this works great, but is there a way to add the info for not just the 4 fields above but all that Outlook export to PST give? Subject    Body    From: (Name)    From: (Address)    From: (Type)    To: (Name)    To: (Address)    To: (Type)    CC: (Name)    CC: (Address)    CC: (Type)    BCC: (Name)    BCC: (Address)    BCC: (Type)    Billing Information    Categories    Importance    Mileage    Sensitivity

I tried adding "Importance" and it works, but I would appreciate if someone could provide the code for the other fields. thank you!!
With excWks
.Cells(1, 1) = "Subject"
.Cells(1, 2) = "Received"
.Cells(1, 3) = "Sender"
.Cells(1, 4) = "Body"
.Cells(1, 5) = "Importance"
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)
excWks.Cells(intRow, 4) = olkMsg.Body
excWks.Cells(intRow, 5) = olkMsg.Importance
This comment was minimized by the moderator on the site
Hi, please check the code below to your needs:
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) = "Body"

.Cells(1, 3) = "Received"

.Cells(1, 4) = "From: (Name)"

.Cells(1, 5) = "From: (Address)"

.Cells(1, 6) = "From: (Type)"

.Cells(1, 7) = "To: (Name)"

.Cells(1, 8) = "To: (Address)"

.Cells(1, 9) = "To: (Type)"

.Cells(1, 10) = "CC: (Name)"

.Cells(1, 11) = "CC: (Address)"

.Cells(1, 12) = "CC: (Type)"

.Cells(1, 13) = "BCC: (Name)"

.Cells(1, 14) = "BCC: (Address)"

.Cells(1, 15) = "BCC: (Type)"

.Cells(1, 16) = "Billing Information"

.Cells(1, 17) = "Categories"

.Cells(1, 18) = "Importance"

.Cells(1, 19) = "Mileage"

.Cells(1, 20) = "Sensitivity"

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.Body

excWks.Cells(intRow, 3) = olkMsg.ReceivedTime

excWks.Cells(intRow, 4) = olkMsg.SenderName

excWks.Cells(intRow, 5) = GetAddress(olkMsg.Sender, intVersion)

excWks.Cells(intRow, 6) = olkMsg.Sender.Type

excWks.Cells(intRow, 7) = GetRecipientsName(olkMsg, 1, 1, intVersion)

excWks.Cells(intRow, 8) = GetRecipientsName(olkMsg, 1, 2, intVersion)

excWks.Cells(intRow, 9) = GetRecipientsName(olkMsg, 1, 3, intVersion)

excWks.Cells(intRow, 10) = GetRecipientsName(olkMsg, 2, 1, intVersion)

excWks.Cells(intRow, 11) = GetRecipientsName(olkMsg, 2, 2, intVersion)

excWks.Cells(intRow, 12) = GetRecipientsName(olkMsg, 2, 3, intVersion)

excWks.Cells(intRow, 13) = GetRecipientsName(olkMsg, 3, 1, intVersion)

excWks.Cells(intRow, 14) = GetRecipientsName(olkMsg, 3, 2, intVersion)

excWks.Cells(intRow, 15) = GetRecipientsName(olkMsg, 3, 3, intVersion)

excWks.Cells(intRow, 16) = olkMsg.BillingInformation

excWks.Cells(intRow, 17) = olkMsg.Categories

excWks.Cells(intRow, 18) = olkMsg.Importance

excWks.Cells(intRow, 19) = olkMsg.Mileage

excWks.Cells(intRow, 20) = olkMsg.Sensitivity

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 GetOutlookVersion() As Integer

Dim arrVer As Variant

arrVer = Split(Outlook.Version, ".")

GetOutlookVersion = arrVer(0)

End Function



Function SMTPEX(Entry As AddressEntry) As String

Dim olkPA As Outlook.PropertyAccessor

On Error Resume Next

Set olkPA = Entry.PropertyAccessor

SMTPEX = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")

On Error GoTo 0

Set olkPA = Nothing

End Function



Function GetAddress(Entry As AddressEntry, intOutlookVersion As Integer) As String

Dim olkEnt As Object

On Error Resume Next

Select Case intOutlookVersion

Case Is < 14

If Entry.Type = "EX" Then

GetAddress = SMTPEX(Entry)

Else

GetAddress = Entry.Address

End If

Case Else

If Entry.AddressEntryUserType = olExchangeUserAddressEntry Then

Set olkEnt = Entry.GetExchangeUser

GetAddress = olkEnt.PrimarySmtpAddress

Else

GetAddress = Entry.Address

End If

End Select

On Error GoTo 0

Set olkEnt = Nothing

End Function



Function GetRecipientsName(Item As MailItem, rcpType As Integer, Ret As Integer, intOutlookVersion As Integer) As String

Dim xRcp As Recipient

Dim xNames As String

xNames = ""

For Each xRcp In Item.Recipients

If xRcp.Type = rcpType Then

If Ret = 1 Then

If xNames = "" Then

xNames = xRcp.Name

Else

xNames = xNames & "; " & xRcp.Name

End If

ElseIf Ret = 2 Then

If xNames = "" Then

xNames = GetAddress(xRcp.AddressEntry, intOutlookVersion)

Else

xNames = xNames & "; " & GetAddress(xRcp.AddressEntry, intOutlookVersion)

End If

ElseIf Ret = 3 Then

If xNames = "" Then

xNames = xRcp.AddressEntry.Type

Else

xNames = xNames & "; " & xRcp.AddressEntry.Type

End If

End If

ElseIf xRcp.Type = rcpType Then

If Ret = 1 Then

If xNames = "" Then

xNames = xRcp.Name

Else

xNames = xNames & "; " & xRcp.Name

End If

ElseIf Ret = 2 Then

If xNames = "" Then

xNames = GetAddress(xRcp.AddressEntry, intOutlookVersion)

Else

xNames = xNames & "; " & GetAddress(xRcp.AddressEntry, intOutlookVersion)

End If

ElseIf Ret = 3 Then

If xNames = "" Then

xNames = xRcp.AddressEntry.Type

Else

xNames = xNames & "; " & xRcp.AddressEntry.Type

End If

End If

ElseIf xRcp.Type = rcpType Then

If Ret = 1 Then

If xNames = "" Then

xNames = xRcp.Name

Else

xNames = xNames & "; " & xRcp.Name

End If

ElseIf Ret = 2 Then

If xNames = "" Then

xNames = GetAddress(xRcp.AddressEntry, intOutlookVersion)

Else

xNames = xNames & "; " & GetAddress(xRcp.AddressEntry, intOutlookVersion)

End If

ElseIf Ret = 3 Then

If xNames = "" Then

xNames = xRcp.AddressEntry.Type

Else

xNames = xNames & "; " & xRcp.AddressEntry.Type

End If

End If

End If

Next

GetRecipientsName = xNames

End Function




Hope this works for you.
Amanda
This comment was minimized by the moderator on the site
How do I get this to automatically recurse into subfolders?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations