Zum Hauptinhalt springen

Wie kann man E-Mails und Anhänge in einer einzigen PDF-Datei in Outlook konvertieren oder speichern?

Author: Siluvia Last Modified: 2025-05-28

Dieser Artikel beschreibt, wie Sie eine E-Mail-Nachricht und alle darin enthaltenen Anhänge in einer einzigen PDF-Datei in Outlook speichern.

E-Mail und Anhänge mit VBA-Code in eine einzelne PDF-Datei konvertieren oder speichern


E-Mail und Anhänge mit VBA-Code in eine einzelne PDF-Datei konvertieren oder speichern

Bitte gehen Sie wie folgt vor, um eine E-Mail mit allen Anhängen in einer einzigen PDF-Datei in Outlook zu speichern.

1. Wählen Sie eine E-Mail mit Anhängen aus, die Sie in einer einzigen PDF-Datei speichern möchten, und drücken Sie dann die Tasten Alt + F11, um das Fenster Microsoft Visual Basic for Applications zu öffnen.

2. Klicken Sie im Fenster Microsoft Visual Basic for Applications auf Einfügen > Modul. Kopieren Sie dann den folgenden VBA-Code in das Modulfenster.

VBA-Code: E-Mail und Anhang in eine einzelne PDF-Datei speichern

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. Klicken Sie auf Extras > Verweise, um das Dialogfeld Verweise zu öffnen. Aktivieren Sie die Kontrollkästchen für Microsoft Excel Object Library, Microsoft Scripting Runtime und Microsoft Word Object Library, und klicken Sie dann auf die Schaltfläche OK. Siehe Screenshot:

the step 1 about saving email attachments as single pdf

4. Drücken Sie die Taste F5 oder klicken Sie auf die Schaltfläche Ausführen, um den Code auszuführen. Dann erscheint ein Dialogfeld Speichern unter. Geben Sie einen Ordner an, um die Datei zu speichern, vergeben Sie einen Namen für die PDF-Datei und klicken Sie auf die Schaltfläche Speichern. Siehe Screenshot:

the step 2 about saving email attachments as single pdf

5. Anschließend erscheint ein Microsoft Outlook-Dialogfeld. Klicken Sie bitte auf die Schaltfläche OK.

the step 3 about saving email attachments as single pdf

Jetzt ist die ausgewählte E-Mail mit allen ihren Anhängen in einer einzigen PDF-Datei gespeichert.

Hinweis: Dieses VBA-Skript funktioniert nur für Microsoft Word- und Excel-Anhänge.


Einfaches Speichern ausgewählter E-Mails in verschiedenen Formaten in Outlook:

Mit der Massenspeicherungsfunktion von Kutools für Outlook können Sie ganz einfach mehrere ausgewählte E-Mails als individuelle HTML-Formatdatei, TXT-Formatdatei, Word-Dokument, CSV-Datei sowie PDF-Datei in Outlook speichern, wie im folgenden Screenshot gezeigt. Laden Sie jetzt die kostenlose Version von Kutools für Outlook herunter!

the step 1 about saving email attachments as single pdf

Verwandte Artikel:


Beste Office-Produktivitätstools

Eilmeldung: Kutools für Outlook startet kostenlose Version!

Erleben Sie die brandneue kostenlose Version von Kutools für Outlook mit über70 unglaublichen Funktionen, die Sie FÜR IMMER nutzen können! Klicken Sie jetzt, um herunterzuladen!

🤖 Kutools AI : Nutzt fortschrittliche KI-Technologie, um E-Mails mühelos zu bearbeiten, einschließlich Antworten, Zusammenfassen, Optimieren, Erweitern, Übersetzen und Verfassen von E-Mails.

📧 E-Mail-Automatisierung: Automatische Antwort (Verfügbar für POP und IMAP) / E-Mails senden planen / Automatische CC/BCC nach Regeln beim Senden von E-Mails / Automatische Weiterleitung (Erweiterte Regeln) / Automatische Begrüßung hinzufügen / Automatisches Aufteilen von E-Mails mit mehreren Empfängern in einzelne Nachrichten ...

📨 E-Mail-Management: E-Mails zurückrufen / Betrugs-E-Mails nach Betreff und anderen blockieren / Duplikate E-Mails löschen / Erweiterte Suche / Ordner organisieren ...

📁 Anhänge Pro: Stapelspeichern / Stapelablösen / Stapelkomprimieren / Automatisch speichern / Automatisch abtrennen / Automatische Komprimierung ...

🌟 Interface Magic: 😊Mehr hübsche und coole Emojis / Erinnern Sie, wenn wichtige E-Mails kommen / Outlook minimieren statt schließen ...

👍 Ein-Klick-Wunder: Allen mit Anhängen antworten / Anti-Phishing-E-Mails / 🕘Zeitzone des Absenders anzeigen ...

👩🏼‍🤝‍👩🏻 Kontakte & Kalender: Kontakte aus ausgewählten E-Mails stapelweise hinzufügen / Eine Kontaktgruppe in einzelne Gruppen aufteilen / Geburtstagserinnerung entfernen ...

Entsperren Sie Kutools für Outlook sofort mit einem Klick. Warten Sie nicht, laden Sie jetzt herunter und steigern Sie Ihre Effizienz!

kutools for outlook features1 kutools for outlook features2