Back on January 3’rd, 2007 (http://arcanecode.wordpress.com/2007/01/03/enhancing-the-save-attachments-outlook-macro/ or http://shrinkster.com/mhm) 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.
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()
To
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.
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!
![[VB References Dialog]](http://arcanecode.files.wordpress.com/2007/02/saveallattachments.jpg)


February 28, 2007 at 9:17 pm
[...] saves attachments for all messages / calendar items, selected or not. To see it, please see my post http://arcanecode.wordpress.com/2007/02/28/saving-all-attachments-in-outlook/ or http://shrinkster.com/mhn. There I have uploaded a new file with both the macro below and the [...]
March 19, 2007 at 3:18 pm
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
July 3, 2007 at 2:20 pm
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.
April 24, 2009 at 9:13 am
Hi Rogelio,
Can you help me to get the entire code pls?
Kedar
July 29, 2007 at 6:54 pm
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
November 2, 2007 at 9:18 am
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)
Loop
——————
December 2, 2007 at 8:44 am
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?
Thanks,
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
March 13, 2008 at 4:58 am
Thank you so much for this macro. It saves me a lot of time.
May 9, 2008 at 2:47 pm
Is there any simpler way to do this for non-techies?
I’m not able to understand all of this macro and certificate business.
Thanks.
June 5, 2008 at 8:16 am
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.
Sincerely
Attila
and thanks for it in advance
June 24, 2008 at 10:12 am
‘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?
Thanks.
Connie
June 24, 2008 at 12:26 pm
I figured it out. No need to answer. I went to the Visual Basic Editor, Tools, References and clicked on Microsoft Scripting Runtime.
July 7, 2008 at 3:40 am
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!
January 14, 2009 at 4:05 pm
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!!
January 21, 2009 at 5:51 am
awesome. Thank you very much indeed
October 13, 2009 at 3:05 am
Thank you very much, this saves me so much time
October 27, 2009 at 4:25 am
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?
Tia,
Nils Gruson