KutoolsforOffice — Eén oplossing, vijf krachtige tools.Meer bereiken met minder moeite.

Hoe converteer of sla je een e-mail met bijlagen op als één enkel PDF-bestand in Outlook?

AuteurSiluvia Wijzigingsdatum

Dit artikel beschrijft hoe u een e-mailbericht, inclusief alle bijlagen, opslaat als één enkel PDF-bestand in Outlook.

Converteer of sla e-mail en bijlagen op naar één enkel PDF-bestanden met VBA-code


Converteer of sla e-mail en bijlagen op naar één enkel PDF-bestanden met VBA-code

Volg de onderstaande stappen om een e-mail met alle bijlagen als één enkel PDF-bestand op te slaan in Outlook.

1. Selecteer een e-mail met bijlagen die u wilt opslaan als één enkel PDF-bestand en druk vervolgens op de toetsencombinatie Alt+F11 om het venster Microsoft Visual Basic for Applications te openen.

2. Klik in het venster Microsoft Visual Basic for Applications op Invoegen > Module. Kopieer vervolgens de onderstaande VBA-code naar het modulevenster.

VBA-code: Sla e-mail en bijlage op naar één enkel PDF-bestanden

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. Klik op Extra > Verwijzingen om het dialoogvenster Verwijzingen te openen. Schakel de opties Microsoft Excel Object Library, Microsoft Scripting Runtime en Microsoft Word Object Library in en klik op de knop OK. Zie schermafbeelding:

stap 1 voor het opslaan van e-mailbijlagen als één PDF

4. Druk op de toets F5 of klik op de knop Uitvoeren om de code uit te voeren. Er verschijnt dan het dialoogvenster Opslaan als. Kies de map waarin u het bestand wilt opslaan, geef het PDF-bestand een naam en klik op de knop Opslaan. Zie schermafbeelding:

stap 2 voor het opslaan van e-mailbijlagen als één PDF

5. Vervolgens verschijnt het dialoogvenster Microsoft Outlook. Klik op de knop OK.

stap 3 voor het opslaan van e-mailbijlagen als één PDF

De geselecteerde e-mail met alle bijlagen is nu opgeslagen in één enkel PDF-bestand.

Opmerking: Dit VBA-script werkt uitsluitend met bijlagen van Microsoft Word en Excel.


Sla geselecteerde e-mails eenvoudig op als bestanden in verschillende indelingen in Outlook:

Met de Bulk Save-functie van Kutools voor Outlookkunt u meerdere geselecteerde e-mails gemakkelijk opslaan als afzonderlijke HTML-indeling-bestand, Txt-indeling-bestand, Word-document, CSV-bestand en ook als PDF-bestanden in Outlook, zoals op onderstaande schermafbeelding wordt weergegeven.Download nu de gratis versie van Kutools voor Outlook!

stap 1 voor het opslaan van e-mailbijlagen als één PDF

Gerelateerde artikelen:


Beste Office-productiviteitstools

Beleef de gloednieuwe Kutools voor Outlook met 100+ ongelooflijke functies!Klik nu om te downloaden!

🤖KUTOOLS AI:Gebruikt geavanceerde AI-technologie om e-mails moeiteloos te verwerken, waaronder beantwoorden, samenvatten, optimaliseren, uitbreiden, vertalen en opstellen van e-mails.

📧E-mailautomatisering: Automatisch antwoorden (beschikbaar voor POP en IMAP), e-mails plannen om te verzenden, automatisch CC/BCC toepassen op basis van regels bij het verzenden van e-mails, automatisch doorsturen (geavanceerde regels), automatisch een begroeting toevoegen en e-mails met meerdere ontvangers automatisch splitsen in afzonderlijke berichten

📨E-mailbeheer: E-mail terughalen, spam-e-mails blokkeren op basis van onderwerp en andere criteria, dubbele e-mails verwijderen, geavanceerd zoeken en mappen organiseren

📁Bijlagen Pro: Batch opslaan, batch loskoppelen, batch comprimeren, automatisch opslaan, automatisch loskoppelen en automatisch comprimeren

🌟Interface-magie: 😊 Nog meer mooie en coole emoji’s / Waarschuwt u bij binnenkomst van belangrijke e-mails / Minimaliseert Outlook in plaats van het programma te sluiten...

👍Eénklikwonderen: Antwoord Allen met Bijlagen / Anti-phishing-e-mails / 🕘Huidige tijd van afzender-zone weergeven...

👩🏼‍🤝‍👩🏻Contacten & agenda: Batch Contact toevoegen vanuit geselecteerde e-mails / Een contactgroep splitsen in afzonderlijke groepen / Verjaardagsherinnering verwijderen

Gebruik Kutools in uw voorkeurstaal – ondersteunt Engels, Spaans, Duits, Frans, Chinees en meer dan 40 andere talen!

Ontgrendel Kutools voor Outlook direct met één klik! Wacht niet langer—download nu en verhoog uw efficiëntie!

kutools for outlook features1kutools for outlook features2

🚀 Eénklikdownload — Ontvang alle Office-invoegtoepassingen

Sterk aanbevolen: Kutools for Office (5-in-1)

Eén klik om vijf installatiebestandentegelijk te downloaden —Kutools voor Excel, Outlook, Word, PowerPointen Office Tab Pro.Klik nu om te downloaden!

  • Eénkliksgemak: Download alle vijf installatiepakketten in één keer.
  • 🚀Klaar voor elke Office-taak: Installeer de invoegtoepassingen die u nodig heeft, precies op het moment dat u ze nodig heeft.
  • 🧰Inbegrepen: Kutools voor Excel / Kutools voor Outlook / Kutools voor Word / Office Tab Pro / Kutools for PowerPoint