Skip to main content

كيفية تحويل أو حفظ البريد الإلكتروني والمرفقات في ملف PDF واحد في Outlook؟

Author: Siluvia Last Modified: 2025-05-30

تتحدث هذه المقالة عن حفظ رسالة بريد إلكتروني وجميع المرفقات الموجودة فيه في ملف PDF واحد في Outlook.

تحويل أو حفظ البريد الإلكتروني والمرفقات إلى ملف PDF واحد باستخدام كود VBA


تحويل أو حفظ البريد الإلكتروني والمرفقات إلى ملف PDF واحد باستخدام كود VBA

يرجى اتباع الخطوات التالية لحفظ البريد الإلكتروني مع جميع مرفقاته في ملف PDF واحد في Outlook.

1. حدد بريدًا إلكترونيًا يحتوي على مرفقات تريد حفظها في ملف PDF واحد، ثم اضغط على مفاتيح Alt + F11 لفتح نافذة Microsoft Visual Basic for Applications.

2. في نافذة Microsoft Visual Basic for Applications، انقر فوق إدراج > وحدة. وبعد ذلك قم بنسخ الكود VBA أدناه في نافذة الوحدة.

كود VBA: حفظ البريد الإلكتروني والمرفق في ملف PDF واحد

Public Sub MergeMailAndAttachsToPDF()
'Update by Extendoffice 2018/3/5
Dim xSelMails As MailItem
Dim xFSysObj As FileSystemObject
Dim xOverwriteBln As Boolean
Dim xLooper As Integer
Dim xEntryID As String
Dim xNameSpace As Outlook.NameSpace
Dim xMail As Outlook.MailItem
Dim xExt As String
Dim xSendEmailAddr, xCompanyDomain As String
Dim xWdApp As Word.Application
Dim xDoc, xNewDoc As Word.Document
Dim I As Integer
Dim xPDFSavePath As String
Dim xPath As String
Dim xFileArr() As String
Dim xExcel As Excel.Application
Dim xWb As Workbook
Dim xWs As Worksheet
Dim xTempDoc As Word.Document

On Error Resume Next
If (Outlook.ActiveExplorer.Selection.Count > 1) Or (Outlook.ActiveExplorer.Selection.Count = 0) Then
    MsgBox "Please Select a email.", vbInformation + vbOKOnly
    Exit Sub
End If
Set xSelMails = Outlook.ActiveExplorer.Selection.Item(1)
xEntryID = xSelMails.EntryID
Set xNameSpace = Application.GetNamespace("MAPI")
Set xMail = xNameSpace.GetItemFromID(xEntryID)

xSendEmailAddr = xMail.SenderEmailAddress
xCompanyDomain = Right(xSendEmailAddr, Len(xSendEmailAddr) - InStr(xSendEmailAddr, "@"))
xOverwriteBln = False
Set xExcel = New Excel.Application
xExcel.Visible = False
Set xWdApp = New Word.Application
xExcel.DisplayAlerts = False
xPDFSavePath = xExcel.Application.GetSaveAsFilename(InitialFileName:="", FileFilter:="PDF Files(*.pdf),*.pdf")
If xPDFSavePath = "False" Then
    xExcel.DisplayAlerts = True
    xExcel.Quit
    xWdApp.Quit
    Exit Sub
End If
xPath = Left(xPDFSavePath, InStrRev(xPDFSavePath, "\"))
cPath = xPath & xCompanyDomain & "\"
yPath = cPath & Format(Now(), "yyyy") & "\"
mPath = yPath & Format(Now(), "MMMM") & "\"
If Dir(xPath, vbDirectory) = vbNullString Then
   MkDir xPath
End If
EmailSubject = CleanFileName(xMail.Subject)
xSaveName = Format(xMail.ReceivedTime, "yyyymmdd") & "_" & EmailSubject & ".doc"
Set xFSysObj = CreateObject("Scripting.FileSystemObject")
If xOverwriteBln = False Then
   xLooper = 0
  Do While xFSysObj.FileExists(yPath & xSaveName)
      xLooper = xLooper + 1
      xSaveName = Format(xMail.ReceivedTime, "yyyymmdd") & "_" & EmailSubject & "_" & xLooper & ".doc"
   Loop
Else
   If xFSysObj.FileExists(yPath & xSaveName) Then
      xFSysObj.DeleteFile yPath & xSaveName
   End If
End If
xMail.SaveAs xPath & xSaveName, olDoc
If xMail.Attachments.Count > 0 Then
   For Each atmt In xMail.Attachments
      xExt = SplitPath(atmt.filename, 2)
      If (xExt = ".docx") Or (xExt = ".doc") Or (xExt = ".docm") Or (xExt = ".dot") Or (xExt = ".dotm") Or (xExt = ".dotx") _
      Or (xExt = ".xlsx") Or (xExt = ".xls") Or (xExt = ".xlsm") Or (xExt = ".xlt") Or (xExt = ".xltm") Or (xExt = ".xltx") Then
        atmtName = CleanFileName(atmt.filename)
        atmtSave = xPath & Format(xMail.ReceivedTime, "yyyymmdd") & "_" & atmtName
        atmt.SaveAsFile atmtSave
      End If
   Next
End If
Set xNewDoc = xWdApp.Documents.Add("Normal", False, wdNewBlankDocument, False)
Set xFilesFld = xFSysObj.GetFolder(xPath)
xFileArr() = GetFiles(xPath)
For I = 0 To UBound(xFileArr()) - 1
    xExt = SplitPath(xFileArr(I), 2)
    If (xExt = ".xlsx") Or (xExt = ".xls") Or (xExt = ".xlsm") Or (xExt = ".xlt") Or _
       (xExt = ".xltm") Or (xExt = ".xltx") Then  'conver excel to word
        Set xWb = xExcel.Workbooks.Open(xPath & xFileArr(I))
        Set xTempDoc = xWdApp.Documents.Add("Normal", False, wdNewBlankDocument, False)
        Set xWs = xWb.ActiveSheet
        xWs.UsedRange.Copy
        xTempDoc.Content.PasteAndFormat wdFormatOriginalFormatting
        xTempDoc.SaveAs2 xPath & xWs.Name + ".docx", wdFormatXMLDocument
        xWb.Close False
        Kill xPath & xFileArr(I)
        xTempDoc.Close wdDoNotSaveChanges, wdOriginalDocumentFormat, False
    End If
Next
xExcel.DisplayAlerts = True
xExcel.Quit
xFileArr() = GetFiles(xPath)
'Merge Documents
For I = 0 To UBound(xFileArr()) - 1
    xExt = SplitPath(xFileArr(I), 2)
    If (xExt = ".docx") Or (xExt = ".doc") Or (xExt = ".docm") Or (xExt = ".dot") Or _
       (xExt = ".dotm") Or (xExt = ".dotx") Then
        MergeDoc xWdApp, xPath & xFileArr(I), xNewDoc
        Kill xPath & xFileArr(I)
    End If
Next
xNewDoc.Sections.Item(1).Range.Delete wdCharacter, 1
xNewDoc.SaveAs2 xPDFSavePath, wdFormatPDF
xNewDoc.Close wdDoNotSaveChanges, wdOriginalDocumentFormat, False
xWdApp.Quit
Set xMail = Nothing
Set xNameSpace = Nothing
Set xFSysObj = Nothing
MsgBox "Merged successfully", vbInformation + vbOKOnly
End Sub

Public Function SplitPath(FullPath As String, ResultFlag As Integer) As String
Dim SplitPos As Integer, DotPos As Integer
SplitPos = InStrRev(FullPath, "/")
DotPos = InStrRev(FullPath, ".")
Select Case ResultFlag
Case 0
   SplitPath = Left(FullPath, SplitPos - 1)
Case 1
   If DotPos = 0 Then DotPos = Len(FullPath) + 1
   SplitPath = Mid(FullPath, SplitPos + 1, DotPos - SplitPos - 1)
Case 2
   If DotPos = 0 Then DotPos = Len(FullPath)
   SplitPath = Mid(FullPath, DotPos)
Case Else
   Err.Raise vbObjectError + 1, "SplitPath Function", "Invalid Parameter!"
End Select
End Function
  
Function CleanFileName(StrText As String) As String
Dim xStripChars As String
Dim xLen As Integer
Dim I As Integer
xStripChars = "/\[]:=," & Chr(34)
xLen = Len(xStripChars)
StrText = Trim(StrText)
For I = 1 To xLen
StrText = Replace(StrText, Mid(xStripChars, I, 1), "")
Next
CleanFileName = StrText
End Function

Function GetFiles(xFldPath As String) As String()
On Error Resume Next
Dim xFile As String
Dim xFileArr() As String
Dim xArr() As String
Dim I, x As Integer
x = 0
ReDim xFileArr(1)
xFileArr(1) = xFldPath '& "\"
xFile = Dir(xFileArr(1) & "*.*")
Do Until xFile = ""
    x = x + 1
    xFile = Dir
Loop
ReDim xArr(0 To x)
x = 0
xFile = Dir(xFileArr(1) & "*.*")
Do Until xFile = ""
    xArr(x) = xFile
    x = x + 1
    xFile = Dir
Loop
GetFiles = xArr()
End Function

Sub MergeDoc(WdApp As Word.Application, xFileName As String, Doc As Document)
Dim xNewDoc As Document
Dim xSec As Section
    Set xNewDoc = WdApp.Documents.Open(filename:=xFileName, Visible:=False)
    Set xSec = Doc.Sections.Add
    xNewDoc.Content.Copy
    xSec.PageSetup = xNewDoc.PageSetup
    xSec.Range.PasteAndFormat wdFormatOriginalFormatting
    xNewDoc.Close
End Sub

3. انقر فوق أدوات > مراجع لفتح مربع الحوار مراجع. تحقق من مربعات Microsoft Excel Object Library و Microsoft Scripting Runtime و Microsoft Word Object Library ثم انقر فوق زر موافق. شاهد لقطة الشاشة:

the step 1 about saving email attachments as single pdf

4. اضغط على مفتاح F5 أو انقر فوق زر التشغيل لتشغيل الكود. بعد ذلك سيظهر مربع حوار حفظ باسم، يرجى تحديد مجلد لحفظ الملف، ثم أضف اسمًا لملف PDF وانقر فوق زر الحفظ. شاهد لقطة الشاشة:

the step 2 about saving email attachments as single pdf

5. بعد ذلك سيظهر مربع حوار Microsoft Outlook، يرجى النقر فوق زر موافق.

the step 3 about saving email attachments as single pdf

الآن تم حفظ البريد الإلكتروني المحدد مع جميع مرفقاته في ملف PDF واحد.

ملاحظة: يعمل هذا البرنامج النصي VBA فقط مع مرفقات Microsoft Word و Excel.


حفظ رسائل البريد الإلكتروني المحددة بسهولة بتنسيقات مختلفة في Outlook:

مع أداة Bulk Save الخاصة بـ Kutools for Outlook، يمكنك بسهولة حفظ رسائل البريد الإلكتروني المحددة المتعددة كملفات فردية بصيغة HTML، ملفات نصية TXT، مستندات Word، ملفات CSV وكذلك ملفات PDF في Outlook كما هو موضح في لقطة الشاشة أدناه. قم بتنزيل النسخة المجانية من Kutools for Outlook الآن!

the step 1 about saving email attachments as single pdf

مقالات ذات صلة:


أفضل أدوات الإنتاجية لمجموعة Office

خبر عاجل: أدوات Kutools لـ Outlook تطلق إصدارًا مجانيًا!

جرّب الآن الإصدار المجاني الجديد كليًا من أدوات Kutools لـ Outlook مع أكثر من70 ميزة مذهلة، متاحة لك مدى الحياة! انقر للتحميل الآن!

🤖 Kutools AI : يستخدم تقنية الذكاء الاصطناعي المتقدمة لإدارة البريد الإلكتروني بسهولة، بما في ذلك الرد، والتلخيص، والتحسين، والتوسيع، والترجمة، وكتابة الرسائل.

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

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

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

🌟 سحر الواجهة: 😊 المزيد من الرموز التعبيرية الجميلة والرائعة / تنبيهك عند وصول رسائل هامة / تصغير Outlook بدلاً من الإغلاق ...

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

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

افتح أدوات Kutools لـ Outlook فورًا بنقرة واحدة. لا تنتظر، قم بالتحميل الآن وزد من إنتاجيتك!

kutools for outlook features1 kutools for outlook features2