Saving All Attachments in Outlook

Back on January 3’rd, 2007 ( or I posted a useful macro that would save attachments on the selected items in a folder. Since then several have written asking for a version that would save all attachments for every item in a folder, whether it was selected or not.

UPDATE: Some users were having problems downloading the code, so I’ve posted it at:

I finally had a chance to experiment, and it turned out to be very easy. To create a version that saves attachments for all items, you only have to change three lines of code.

The first line is the name of the sub. I changed

Public Sub SaveAttachments()


Public Sub SaveAllAttachments()

Easy enough. Next, the variable declaration of

Dim Sel As Outlook.Selection

Has to be changed to

Dim Sel As Outlook.Items

The final change is to

Set Sel = Exp.Selection

It becomes

Set Sel = Exp.CurrentFolder.Items

And that’s it, it works. Let me explain what’s happening. In the original, I was cycling through the Selection collection of the Outlook Explorer object. The Selection is a special type of Items collection that holds what’s selected.

For our new macro we wanted all of the items in the current folder, not just what was selected. The first thing we had to do, after changing the sub’s name, was to change the data type of the Sel variable. I wanted to get away from the specific selection collection to the more generic items collection.

Then, all I had to do was have the Sel point to the Items collection of the current folder. Since both Selection and CurrentFolder.Items both support the Items interface, everything else just worked. That’s the power of OOP!

I’ve uploaded a new version of the file as Save All Attachments. It contains this version, the original SaveAttachements, and the GetOutputDirectory function. If you are installing this new, please don’t forget to set a reference to the Microsoft Scripting Runtime Library (scrrun.dll).

Select Tool, References from the VB Macros Editor. Scroll down until you find Microsoft Scripting Runtime, and check it. You’ll know it’s the right one when the file name is scrrun.dll.

[VB References Dialog]

I’ve tested this with messages stored in a variety of folders, including the Inbox. It’s based off the current folder, so it doesn’t seem to care where you are at. I’ve also gotten this to work with the calendar as well.

There you go, you now have a choice. Using my original macro you can save attachments for only the selected items in the current folder, or using this version you can save all attachments for all items in the current folder. I hope the folks who requested the change will find this fits their needs, enjoy!



26 thoughts on “Saving All Attachments in Outlook

  1. Would it be possible to update this macro to save the attachment as the subject with the attachment’s original extnesions?

    I just finished scanning in a ton of documents from our pdf scanner and now I have a ton of Document.pdf files in an e-mail folder with the subject being the filename I want it to actually be.

    I’d rather not have to manually rename all of these files if I don’t have to, because there are a lot of them. Thanks

  2. Here is the deal to get the subject of the e-mail:
    This is part of the original code, the last 4 lines are the ones you have to attach to the code, the other ones are hints to find where this lines are going to be located

    For cnt = 1 To Sel.Count
    ‘If the e-mail has attachments…
    If Sel.item(cnt).Attachments.Count > 0 Then
    Set elementoactual = Sel.item(cnt)
    Dim asunto As String
    asunto = elementoactual.Subject
    asunto = asunto + ” ”

    ‘ANOTHER LINE MODIFICATION, for this thing to make it work you have to attach the second line of the following code

    Do While fileExists = True
    outputFile = asunto + outputFile
    outputFile = InputBox(“The file ” + outputFile _
    + ” already exists in the destination directory of ” _
    + outputDir + “. Please enter a new name, or hit cancel to skip this one file.”, “File Exists”, outputFile)

    ‘ So that´s it, hope you enjoy it, greetins from Mexico, VIVA MEXICO CABRONES!!!!!

    ‘If you still don´t know how to do it, e-mail me and i will send you the entire code so you don´t get any trouble attaching lines.

  3. Old VB code doesn’t die, it just gets googled!

    thanks, within 2 minutes of finding your code, I saved 172 attachments from 90 emails…
    Pity i have to read those attachments now 😦

  4. I have 100’s of PDF scans sent by a printer/scanner, all using the same “scan001.pdf” filename.

    In case anyone has the same situation as me, I modified the loop a bit to automatically save attachments with the same name by attaching a sequence number to the end rather than asking for a new name.

    The result is “scan001.pdf 0.PDF”, “scan001.pdf 1.PDF”, etc

    This is a quick hack and leaves the filenames with extra extension characters, but it works fine for my needs. I rename them manually once I view them.
    ‘ Sequence Number for matching filenames
    Dim sequenceNumber As Integer
    Dim fileExtension As String

    sequenceNumber = 0
    fileExtension = “.PDF”
    Do While fileExists = True
    ‘ prompt for new filename (original code)
    ‘outputFile = InputBox(“The file ” + outputFile _
    ‘ + ” already exists in the destination directory of ” _
    ‘ + outputDir + “. Please enter a new name, or hit cancel to skip this one file.”, “File Exists”, outputFile)

    ‘ new code to just append a sequence number at the end for multiple mails using the same filename
    ‘ time and data stamp of the attachment are preserved
    outputFile = (outputFile + “.” + Str$(sequenceNumber) + fileExtension)
    sequenceNumber = sequenceNumber + 1

    ‘If user hit cancel
    If outputFile = “” Then
    ‘Exit leaving fileexists true. That will be a flag not to write the file
    Exit Do
    End If
    fileExists = fso.fileExists(outputDir + outputFile)

  5. Hi

    Sorry, I didn’t know how to contact you so I decided to post it here since it regards to attachments.

    I have incoming emails (a lot of) that has one attachment HTML file (newsletter).
    The attached newsletter contains (at the bottom) an email address link of a user (A tag).
    I need to extract that address and unsubscribe the user from one of my web applications.

    Up until now, using Exchange 2003 & outlook 2007 I was able to do it with a macro.
    I saved the attachment to a text file on disk and then parsed it to extract the email address.
    Later on I converted it to a PowerShell script.

    When I switched to Exchange 2007, things started to go wrong.
    Now, when I save the attachment to a text file, the contents looks like binary encoding.

    I also posted this question in the Outlook.general and Exchange.admin newsgroups, Nothing returned 😦
    Is there a way to *fix* it? How can I save the attachment to a text file?


    Shay Levi
    $cript Fanatic

  6. Is there any simpler way to do this for non-techies?

    I’m not able to understand all of this macro and certificate business.


  7. Dear expert,

    I do not know much about VBA programming and I hope You can help me by modifying the sample You have put on this site.
    I would like to ask some help in saving attachment with the following condition:

    1. I got mails every day with attachments and I would like to save only attachments of mail messages for that day (not all, and not from previous dates) from a subfolder named “MEAS” in Inbox.

    2. I usually select each message one by one, and would like to run the macro for each message. Message titles contain date (if it is important..) and a few letter for further identification like Meas1 +date, Meas2 +date.

    3. I would like to save attachments of each mail to C:/Attachment/2008 (no rest exists..) so:
    with creating folder in folder 2008 like: 2008.06. as a subfolder,
    than create another subfolder for the day the mail received like 06.10 (for example)
    and saving each message attachments to further subfolders named by the letter in the title of the message for example Meas1.
    So if the message title contains Meas1, the saving destination subfolder in that day’s folder would become AttachMeas1Saved.

    I hope could explain it well and I would highly appreciate if You could help me with this matter.

    Thank You in advance again.



    and thanks for it in advance

  8. ‘Requires reference to Microsoft Scripting Runtime (SCRRUN.DLL)
    Dim fso As FileSystemObject

    I ran into a compile error at this point in the macro. What do I need to do?

  9. I figured it out. No need to answer. I went to the Visual Basic Editor, Tools, References and clicked on Microsoft Scripting Runtime.

  10. I’m running the script on all my work mail, but I’m getting the following runtime error: ‘-2147024809 (80070057)’: Cannot save the attachment. When I click on debug it brings me to the att.SaveAsFile (outputDir + outputFile) line. Seems to happen after the 66th attachment has been downloaded. Any ideas? Thank you!

  11. I’m trying to save the attachments for the mail incoming. Is it possible to run the macro that saves attachments for every mail in ?
    Thanks in advance!!

  12. Have you any idea how to save password protected attachments to disk?
    I receive mails with attached password protected CSV files.
    Now I’m trying to write a piece of VBA code to save these files to disk, but the SaveToFile call on these attachments obviously fail because I didn’t provide a password. But I can’t find where to do this anyway.
    Or is there any way to open these attachments without saving them to disk first with the Excel Object Model and go from there?

    Nils Gruson

  13. Hi,

    Can you please help to retrieve all the attachment from a particular folder and save it in C:\SCB_Attachment.

    Note: Sorry, could you please CC: a copy of the coding in the below email.

    Thanks Inadvance.

    Best Regards,
    Vinod Kumar. S

  14. Hi,

    Am also having a problem in the coding while running the macro “Set att = Sel.Item(cnt).Attachments.Item(AttachmentCnt)” getting type mismatch error msg and also i tried changing “Set Sel = Exp.CurrentFolder.Items” & “Dim Sel As Outlook.Items” as per your instrution.


  15. Hello, this is an excellent site. 10 point for the programmer. Also, I have been looking for years a tool to allow me to compress a folder or file, split it in specific size an email it on separated emails to the same address. Very useful when sending big files to a limited inbox. All in one single sequence. Please help. My knowledge in programing will take me time to reach this target.

  16. Hi arcane code,

    I’m running into an issue while downloading the macro to my computer when I click the link you gave it says “You must both be a user of this blog as well as be currently logged into” I’m logged on already. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s