I spent my new years day recovering from a nasty cold. My wife asked me if there was a way to save the attachments in Microsoft Outlook for multiple messages. I spent a little time and came up with a handy macro, I thought others might find it useful as well.
First, in Outlook click on Tools, Macro, Visual Basic Editor. Now in the editor on the left you’ll see Project. Drill down Project1, Microsoft Office Outlook, ThisOutlookSession, and paste the code below in:
Public Sub SaveAttachments()
‘Note, this assumes you are in the a folder with e-mail messages when you run it.
‘It does not have to be the inbox, simply any folder with e-mail messages
Dim App As New Outlook.Application
Dim Exp As Outlook.Explorer
Dim Sel As Outlook.Selection
Dim AttachmentCnt As Integer
Dim AttTotal As Integer
Dim MsgTotal As Integer
Set Exp = App.ActiveExplorer
Set Sel = Exp.Selection
‘Loop thru each selected item in the inbox
For cnt = 1 To Sel.Count
‘If the e-mail has attachments…
If Sel.Item(cnt).Attachments.Count > 0 Then
MsgTotal = MsgTotal + 1
AttTotal = AttTotal + Sel.Item(cnt).Attachments.Count
‘For each attachment on the message…
For AttachmentCnt = 1 To Sel.Item(cnt).Attachments.Count
‘Get the attachment
Dim att As Attachment
Set att = Sel.Item(cnt).Attachments.Item(AttachmentCnt)
‘Save it to disk
att.SaveAsFile (“C:\Attachments\” + att.FileName)
Next
End If
Next
‘Clean up
Set Sel = Nothing
Set Exp = Nothing
Set App = Nothing
‘Let user know we are done
Dim doneMsg As String
doneMsg = “Completed saving ” + Format$(AttTotal, “#,0″) _
+ ” attachments in ” + Format$(MsgTotal, “#,0″) + ” Messages.”
MsgBox doneMsg, vbOKOnly, “Save Attachments”
Exit Sub
ErrorHandler:
Dim errMsg As String
errMsg = “An error has occurred. Error ” + Err.Number + ” ” _
+ Err.Description
Dim errResult As VbMsgBoxResult
errResult = MsgBox(errMsg, vbAbortRetryIgnore, _
“Error in Save Attachments”)
Select Case errResult
Case vbAbort
Exit Sub
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
End Sub
Note some browsers trash the html I tried to do above, so I uploaded it as a text file. Just save to your hard drive and paste into the VBScript Editor or rename the txt to cls and do a File, Import and browse to this file: https://arcanecode.files.wordpress.com/2007/01/saveattachments1.txt
And of course save it. Now switch back to Outlook, and click on View, Toolbars, Customize. Click on the Toolbars tab, and click New. I named my new toolbar ArcaneCode, but name yours what you will.
Once you have the new toolbar, click on the Commands tab. Scroll down on the left to Macros and click on it. You should see your new macro in the Commands window on the left. Drag it on to your new toolbar. Now you can shorten the name a little, right click on the tool, to see it’s pop up menu. Go to Name and click on it, then shorten the name to what you want. I then drug my new toolbar up with the rest of my other toolbars.
Two notes, I made it easy on myself and am saving all attachments to C:\Attachments, which I’ve hardcoded in the macro. Feel free to change to what you want or add code to have it ask you for the folder.
Second, I have tested with Outlook 2002 and 2003, but have not tested under 2007. Your milage may vary.
And there you go, a way to save attachments on all the messages you have selected within Outlook.
Hi,
your code is simply great!
I like it but it is not working when I need to save attachements for all messages in inbox, whatever they are selected or not.
I am not able to automatically select all messages (with a macro) and then save the attachments (with your macro). Any idea?
Thanks,
P.
I’m working in C#, updating an application I first wrote several years ago so it will work with Office 2007 At the time, I used CDO 1.21 (aka MAPI) to save attachments to a folder. I’m now using the Outlook 12.0 Object Library. When I use SaveAsFile, it works fine, unless the filename is too long. If the filename is too long, the file shows up in Windows Explorer, but its locked somehow so it cannot be opened. If I try to open it (by double-clicking on the filename), I get a File Not Found error. For an Excel file, there’s a problem once the filename length gets to around 30 characters. In Word, it happens around 85 characters.
I had a similar problem with MAPI, but addressed it with this code:
MAPI.Fields aFields = (MAPI.Fields) mAttach.Fields;
MAPI.Field aField = (MAPI.Field) aFields.get_Item(MAPI.CdoPropTags.CdoPR_ATTACH_LONG_FILENAME, miss);
sAttName = aField.Value.ToString();
I would then save the file under the sAttName name using this code:
oAttachPath = Application.StartupPath + @”\AttachHold\” + sAttName;
mAttach.WriteToFile(oAttachPath);
I can’t find any similar code in the Outlook Object Library to set an acceptable name that will save the file properly.
Any thoughts, or am I the only one that’s having the problem, so it’s probably something else in my code that’s screwed up? (If I can’t find a better solution, I’m guessing I can just save each file using a short generic name, then rename the file.)
Thanks.
UPDATE: I tried the alternative method of using a temp name and then renaming the file back to the original name. It didn’t work.
Paulus, check out my Feb 28th post at http://shrinkster.com/mhn, it has the macro you requested to save all attachments for all items, selected or not.
Jon, wish I had some idea, but I don’t, haven’t had a lot of Office 2007 experience yet. Just out of curiosity, save the long file name, then reboot your machine, then see if you can open the file. It could be Office is holding the lock and preventing everything else from opening it. Rebooting should free all the locks and tell you if that’s the issue.
Arcane
wow this macro is awesome… pls do share more such good macros with me… pls mail me the weblinks for such maros
Hi Raghu,
You didn’t leave an e-mail so I can’t e-mail you, but if you check out my posts for January 3rd and February 28th 2007 you’ll find an enhanced versions of the above macro, and an alternate version that saves for all items without forcing them to be selected first.
Arcane
hi,
by any chance this whole thing can run automatically? what i mean is that it is possible to save the attachment from emails from a particular sender everytime a new mail enter the mailbox?
thanks,
Jackie
thanks
Just a note, **if the user is pasting into the VB code editor**, the code you provided in the TXT file has to have everything up to:
Public Sub SaveAttachments()
removed before it works.
I ran into a problem trying to save msg files that where sent to me as attachments. Many of the e-mail attachments have the same name. Can the script be modified to auto rename or append a counter to the file name so all the attachments are saved?
Gus
Hi. Thanks for great code. It was almost what I was looking for, but I’m having a bit of trouble getting my own script to work as I want. I’ve made a macro (VB) for outlook that takes the active item (message), renames it to “date & subject” then copies it to a folder.
This works fine. But I want to get the “SaveAs” function to work, so the user can select the destination folder them selves. Do you have something that can pop-up the save as dialog and then use the selected target folder to set destination-folder in a script?
My e-mail: jta@sweco.no
I’m trying to view your text file for this macro but WordPress keeps telling me I need to be a user of this blog and logged in to WordPress. Even though I logged in, I don’t know what else to do. I would love to get the text version because copying the code from this page is trashing the macro. Thanks!