Combine Outlook Attachments Into One PDF

Posted on 2009-05-05
Last Modified: 2012-05-06
I am looking for a fast way to convert attached pictures into one pdf.  Currently, the only way I can think of is to select all of the files, copy them, save them to a folder and then do the combine from there.  Is there a way to combine all pics into one pdf through the use of a one-click style macro? Thanks for reading.
Question by:thedeal56
  • 6
  • 5
LVL 76

Expert Comment

by:David Lee
Comment Utility
Hi, thedeal56.

The answer to that question depends on what software you have available for combining the pictures and whether it can be automated.  It's definitely possible to write a macro to do something like this assuming that you have suitable software and that software supports automation.  I've never had the need to do anything like this, so I've no idea what software you'd use to accomplish combining the pictures.

Author Comment

Comment Utility
Are you talking about acrobat?  That's what I'm using to combine multiple photos into one pdf.  
LVL 76

Expert Comment

by:David Lee
Comment Utility
Since you're using Acrobat, that's the tool I'm talking about.  I'm not an Acrobat expert and don't know of a way to script creating a PDF from a series of pictures.  I can handle the Outlook portion of this, saving the pictures to disk and calling some process that combines the pictures into a PDF.  It's that other process that I'll need help with.  If you know of a script or batch process that does this, then I can help.  Otherwise, the best I can do is save the pictures to disk.

Author Comment

Comment Utility
So, you're saying that you can help with the part of the macro that saves the pics to a folder, but not with the part that converts them into a single pdf file?  It sounds like I may be looking at running two batch processes.  One to move the files out of outlook, and one to combine the files.  Is that right?  I was hoping to settle this all with one outlook macro, so if it can't be done that way, can you show me how to do the following?:

What I need the macro to do:

1.  Create a folder which has the name of the subject of the email
2.  Save all attached pictures to the newly created folder.

Thanks for helping me with this.
LVL 76

Expert Comment

by:David Lee
Comment Utility
"you can help with the part of the macro that saves the pics to a folder"

"not with the part that converts them into a single pdf file"
Not without some guidance.  Combining the pictures into a single PDF file doesn't have anything to do with Outlook.  I'll be glad to help with that if you know how to automate the process.  For example, if you know this can be done with a command-line command and you know what the command is, then I can right a script that will save the files and run that command.  But, if you're as much in the dark as I am about how to perform that step outside of the GUI, then I can't help.

"One to move the files out of outlook, and one to combine the files."
It doesn't have to be if you know what the batch process for combining the files and producing a PDF is.  Saving the files is an Outlook process, combining them and producing a PDF isn't.  

How do you want to run the macro?  Do you want to select the messages and then run it?
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


Author Comment

Comment Utility
I'm going to check around on the possible command line pdf combine.  I would like to avoid selecting all the files in the email.  If it's possible, can the macro assume that the open email's attachments are the ones I want to save to the folder?
LVL 76

Expert Comment

by:David Lee
Comment Utility
Yes, that's possible.  I'll hold up on this until I hear back from you on the pdf aspect.

Author Comment

Comment Utility
I think I have a solution for the pdfs.  It's probably not the best way, but let me know what you think.  There is a program called OmniFormat that monitors a folder's contents, and attempts to convert anything in the folder into a pdf.  This process will happen on its own; I'll just need to set the monitored folder to whichever directory the files are initially saved to.  Once the files are in the folder (Let's use C:\PDF), we can use PDF Tool Kit (PDFTK) to do the actual combining.  

Here's a command line example of the combine:

--> pdftk c:\pdf\1.pdf c:\pdf\2.pdf c:\pdf\3.pdf cat output c:\[NAME OF EMAIL SUBJECT]\123.pdf

So, here's how I'm guessing the macro needs to work.

1. Delete all files in C:\PDF
2. Copy the open email's files to C:\PDF
3. Make a directory similar to this: C:\CombinePDF\[NAME OF EMAIL SUBJECT]
4. Run the combine command: pdftk c:\pdf\1.pdf c:\pdf\2.pdf c:\pdf\3.pdf cat output C:\CombinePDF\[NAME OF EMAIL SUBJECT]\123.pdf

The only problem I can foresee is the possibility of the combine taking place before all the images are converted to .pdf.  If it's not too much trouble, maybe adding some type of delay between steps 3. and 4. would come in handy.  I'll have to do some testing to see how long the conversions actually take, so I could adjust the delay as needed.  Thanks again.  

LVL 76

Accepted Solution

David Lee earned 500 total points
Comment Utility
Looks like a good solution.  I wrote the script to monitor the folder for the output files before running the combine command.  That'll prevent the combine command from running before the individual PDFs are produced.  I could not test the entire process since I don't have the software that monitors a folder and creates the individual PDFs, nor the tool for joining the PDFs.  

Follow these instructions to set this up.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor

To use this

1.  Open a message
2.  Run the macro
Sub JoinPDFs()

    'Edit the folder paths on the next two lines.  Ensure that they end with a backslash character.'

    Const PDF_FOLDER = "C:\PDF\"

    Const COMB_FOLDER = "C:\CombinePDF\"

    Dim olkMsg As Outlook.MailItem, _

        olkAttachment As Outlook.Attachment, _

        objFSO As Object, _

        objFolder As Object, _

        objFile As File, _

        objShell As Object, _

        colNames As New Collection, _

        strName As String, _

        strFilenames As String, _

        varName As Variant


    '---- Setup

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objShell = CreateObject("WScript.Shell")


    '--- Step 1: Clear the PDF folder'

    Set objFolder = objFSO.GetFolder(PDF_FOLDER)

    For Each objFile In objFolder.Files

        objFile.Delete True



    '---- Step 2: Get the current message and save all attachments to the PDF folder'

    Set olkMsg = Outlook.Application.ActiveInspector.CurrentItem

    For Each olkAttachment In olkMsg.Attachments

        olkAttachment.SaveAsFile PDF_FOLDER & olkAttachment.FILENAME

        strName = objFSO.GetBaseName(olkAttachment.FILENAME) & ".pdf"

        colNames.Add strName, strName

        strFilenames = strFilenames & strName & " "



    '---- Step 3: Make a folder for this combine operation'

    Set objFolder = objFSO.CreateFolder(COMB_FOLDER & RemoveIllegalCharacters(olkMsg.Subject))


    '----Step 4: Wait for the PDF files to all be produced'

    Do Until colNames.Count = 0


        For Each varName In colNames

            If objFSO.FileExists(PDF_FOLDER & varName) Then

                colNames.Remove varName

            End If




    '---- Step 5: Combine the files'

    objShell.Run "pdftk " & strFilenames & "cat output " & objFolder.Path & "\123.pdf"


    '---- Clean-up'

    Set objShell = Nothing

    Set objFSO = Nothing

    Set objFolder = Nothing

    Set objFile = Nothing

    Set colNames = Nothing

    Set olkAttachment = Nothing

    Set olkMsg = Nothing

End Sub

Open in new window


Author Comment

Comment Utility
Hey, that works great.  Thanks for all your help.
LVL 76

Expert Comment

by:David Lee
Comment Utility
You're welcome.

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Outlook won't display email details 5 30
Publishing OWA on TMG 2010 2 22
outlook 4 21
Searching Outlook Email Archives 5 4
Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now