Rule to automatically save attachment in Outlook
Here's a cool trick. If you receive a lot of email attachments, maybe it would be useful to save them to My Documents or some other location on your computer besides keeping them in Outlook. I can show you how to do exactly that! These instructions show you step by step how to automatically save attachments of emails as they arrive. I have tested this in Outlook 2007 and 2010.
- Open the VBA IDE in Outlook. Alt-F11 will do this.
- Insert the following code to the Modules section. On the left side there is a tree, expand until you find Modules. Then, if there is not a Module item under Modules, create one by right clicking on Modules. Or right click and choose Insert -> Module.
- Now, paste the text below in the main VBA window.
- Close the VBA IDE.
- Create a Rule that calls the script. Tools -> Rules and Alerts -> New Rule...
- In the first screen of the new rule wizard, choose "Check messages when they arrive".
- In the second, you could specify certain criteria that the message must match. Tip: Try "with specific words in the message header" and ".txt" for the search string to save only .txt files.
- On the third screen, choose "run a script". When you click the underlined word, "script", you should see the code that you pasted in the VBA console.
- Click "finish", and test your work.
If this was helpful to you, drop me a note in the comments below.
Public Sub saveAttachtoDisk (itm As Outlook.MailItem) Dim objAtt As Outlook.Attachment Dim saveFolder As String saveFolder = "c:\temp\" For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
If you want to prevent files from being overwritten - for instance, if you receive two emails with the same file name, and don't want the second email to overwrite the attachment that you saved from the first, add this code below the lines that begin with 'dim':
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
Then replace the line that begins with 'objAtt.SaveAsFile' with this:
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
If you want to save attachments of a certain kind only (for example, XML files), you can use this bit of code inside of the "For Each" loop (instead of the code provided above):
if InStr(objAtt.DisplayName, '.xml') Then
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayNameend if
Troubleshooting
If you can't get the rule to work, try adjusting your Outlook security settings:
-
On the File tab, choose Outlook Options to open the Outlook Options dialog box, and then click Trust Center.
-
Click Trust Center Settings, and then the Macro Settings option on the left.
-
Select Notifications for all macros and then click OK. The option allows macros to run in Outlook, but before the macro runs, Outlook prompts you to verify that you want to run the macro.
-
Restart Outlook for the configuration change to take effect.
Link: http://msdn.microsoft.com/en-us/library/ee814736.aspx
In current versions of Outlook, "run a script" is disabled by security
updates. The following page tells how to re-enable scripts for different versions of Outlook.
https://www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/
Comments
LBH (not verified)
Mon, 01/17/2011 - 02:43
Permalink
It worked nice for me initially...
It worked nice for me initially, but then not sure why I cant find the attachment in the dedicated folder anymore. Still can’t figure out.
LBH (not verified)
Tue, 02/01/2011 - 02:32
Permalink
It works now...
I moved the code to 'ThisOutlookSession' and it is working now...
Quirk (not verified)
Wed, 02/09/2011 - 10:38
Permalink
Genius!
I get daily backups from cron and this has saved me hours of filing, thanks!
Hafierro (not verified)
Sat, 02/12/2011 - 10:15
Permalink
Muy bien
Esta muy facil y lo mejor de todo "G R A T I S"
Gracias.
Anonymous (not verified)
Thu, 03/17/2011 - 18:04
Permalink
This is just awesome.
This is just awesome.
I haven't created the module under "This Outlook Session". Still, I think its working well.
Thanks for the solution
Anonymous (not verified)
Thu, 03/24/2011 - 22:57
Permalink
Well, I had left a comment on
Well, I had left a comment on 03/17/2011 regarding the above code. For some reason, its not working now and I can't figure out why!
Dave (not verified)
Wed, 03/30/2011 - 12:57
Permalink
chang attachment file name
is there a way to have the file name change - like to the date and time? the attachments all have the same file name.
CC (not verified)
Mon, 04/18/2011 - 15:21
Permalink
You are the best!
What a time saver!
Mohamed Ireshad... (not verified)
Fri, 04/29/2011 - 21:14
Permalink
Rule to autosave attachment in Outlook
It worked and really thankful for your efforts.
maharajan (not verified)
Wed, 12/28/2011 - 00:22
Permalink
dear All
can you send me the working script please ?
ozlozza (not verified)
Mon, 05/16/2011 - 21:19
Permalink
I bow to thee!
True legend! this is great
esteban (not verified)
Tue, 05/17/2011 - 15:27
Permalink
Thank You
Thank You very much. I have an acquisition system sending daily emails with data. This script saves me a lot of time.
ozlozza (not verified)
Wed, 05/18/2011 - 21:50
Permalink
stopped working
Hi this worked great the first day with setting up the rule, however it won't work now even if I open up the rule and make it run. I tried deleting and recreating the rule but didn't fix it. I have created 3 modules and 3 rules for different application emails containing reports that run each day.
northben
Tue, 06/21/2011 - 16:46
Permalink
I'm not sure what to tell you
I'm not sure what to tell you, ozlozza. You could try deleting all of the modules and recreating them. I'm far from an expert on Outlook VBA.
Joshy (not verified)
Tue, 06/21/2011 - 09:43
Permalink
works - halfly
Hi, this works alright, but the attachments are not deleted from the mails. This does not help in my case. How to auto-delete the attachment inside the mail?
northben
Tue, 06/21/2011 - 16:21
Permalink
Joshy,
Joshy,
Create a new line just before "Set objAtt = Nothing" and put this code in the new line:
objAtt.Delete
Ashley (not verified)
Wed, 06/22/2011 - 11:55
Permalink
Amazing!
Thank you so much!! This will save me so much time!! I just ran it on message already in my Inbox and it worked beautifully. I
(Now, is there a way to automatically delete messages or move them to deleted items as they arrive instead of just copying them to a folder?)
northben
Wed, 06/22/2011 - 12:08
Permalink
Sure, Ashley, you can delete
Sure, Ashley, you can delete the entire message.
In the Rules Wizard, where you click the action to "run a script", also select to "move it to the specified folder".
Then select the "Deleted Items" folder in the lower half of that window. Note: I tried simply using the action "delete it" but that didn't work. I suspect that Outlook deleted the email before it tried to run the script. However, should will work just as well.
Ashley (not verified)
Wed, 06/22/2011 - 13:03
Permalink
Deleting When Sending and Copying Distribution List
Ben, wow, thanks for the quick reply! I apologize; I didn't specify that my question was related to a different task. Essentially, most of the messages I send copy a distribution list (of which I am also a part), so the message is in my sent mail and also my inbox. It drives me crazy when I send a message then have to delete it because it appears in my inbox as a new message as soon as I send it. I've tried rules for deleting or moving messages when I am in the sender's address and I've tried to create my own macro (but failed miserably as I am definitely no VBA pro) and they still go to my inbox. I don't know what I'm doing wrong. Sorry for this unrelated post. And thanks once again for your original macro!!
northben
Thu, 06/23/2011 - 16:57
Permalink
Ashley
Ashley,
I researched your question and found an easy way to accomplish this. I thought it may be useful to others, so I wrote about it. Let me know if you have any questions.
Ashley (not verified)
Thu, 06/23/2011 - 18:27
Permalink
I can't thank you enough!
Ben,
THANK YOU! I don't know what Bitcoins are, but I will investigate because I definitely owe you! I also feel like a dumba#$ because I should have been able to figure that out. Anyway, thank you, thank you, thank you!! :-D
northben
Thu, 06/23/2011 - 18:32
Permalink
Ashley
You're welcome. I'm glad to help out. You don't need to give me any bitcoins though. :)
Jacob (not verified)
Thu, 06/30/2011 - 07:32
Permalink
Also many thanks from me.
Also many thanks from me.
I've just made a window-service to auto-print PDF-files.
Now i can print these file directly when they arive by mail
Raivis (not verified)
Sun, 07/17/2011 - 07:53
Permalink
End of statement expected
Hello guys!
For some reason it does not work for me. In this line: Dim saveFolder As StringsaveFolder = "C:\temp\" it selects the "=" and says that End of statement expected. Is there any workaround?
Have a great weekend!
Raivis (not verified)
Sun, 07/17/2011 - 09:17
Permalink
End of statement expected
I changed the code to this one:
Dazza (not verified)
Tue, 07/19/2011 - 03:02
Permalink
Small change to original code needed
Try this small change - I had the same problem but borrowed the method from some other code
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Const saveFolder As String = "c:\Temp\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Chelle (not verified)
Tue, 07/19/2011 - 17:59
Permalink
Can't get this to work
I copied the code exactly as:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As StringsaveFolder = "c:\temp\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
I got the same error message as Ravis, so I tried the code change that Dazza recommended. I created a temp folder on my C: drive, created the Outlook rule, and it is still not working. I am at a loss as to what I am doing wrong. I did name the VBE module as "SaveAttachments". Does that have any impact? Additionally, the rule I set up looks like:
Apply this rule after the message arrives
which has an attachment
and on this machine only
run Project1.saveAttachtoDisk
Any help is appreciated!!!!
northben
Tue, 08/02/2011 - 21:13
Permalink
Chelle,I don't have Outlook
Chelle,
I don't have Outlook in front of me, but I think you need to put the code in "Module 1" or something similar. If that doesn't help, try checking your Outlook security settings - Outlook might be preventing VBA code from running. If you aren't sure where to look, try Googling something like "Outlook 2007 VBA security settings".
I'm not sure, but I bet your problem is related to the security settings.
Mike (not verified)
Mon, 08/08/2011 - 10:44
Permalink
Convert PDF...
Ben,
Thanks for the code. Works great. But I need to take it one step futher. I receive a daily email with a pdf file. I am using your code to automatically save the file to a shared folder. Our web guy runs a process that moves the file out to our web server but he needs the file converted to an html file first. Currently I have to manually open the pdf file and save-as to an html file. I know this is outside of the scope of your original code but are you aware of any way to automatically convert a pdf attachment to html, either during the original save or after it arrives in the shared folder?
Thanks... Mike
Shezad (not verified)
Sun, 08/28/2011 - 01:57
Permalink
Thanks
This is going to save me and my time COUNTLESS hours that we spend each month
Do share any other scrips with us.
Thank you
Shezad (not verified)
Sun, 08/28/2011 - 04:05
Permalink
Query
I'm facing a problem.
The code cannot handle files with duplicate names so it replaces the old one with the new file without any prompt.
Appreciate your assistance.
Benjamin (not verified)
Wed, 08/31/2011 - 10:28
Permalink
Thanks, works nice did a few
Thanks, works nice did a few tweaks to check if a file with the same name exists. Using it to automatically extract log files from error/usage reports for some in house software we develop.
FDMILNER (not verified)
Fri, 09/09/2011 - 16:38
Permalink
Works also with Outlook 2010
Thanks for sharing, it works also with Outlook 2010. The only drawback is that it will only work when Outlook is running, but I can get past that by sending the emails to a "tasks" computer that is always logged in.
achraf (not verified)
Tue, 09/13/2011 - 08:33
Permalink
the code source
<p>Hey ben,</p><p>Can't find the link to your vba code !</p><p>Thx,</p><p>Achraf</p>
northben
Tue, 09/13/2011 - 12:53
Permalink
I put the code in quotes so
I put the code in quotes so it is not mixed in with the rest of the blog post. It's at the end, just above the comments.
Reg O (not verified)
Wed, 09/21/2011 - 06:03
Permalink
Excellent
You are soooooo clever :)
This has solved a big problem for me and my users who use a MFC and their scans only goto e-mail.
Thank You so much
Reg
Great Yamouth, England
I used reply cus the comment field wasn't active on my main screen.
Greg (not verified)
Tue, 09/27/2011 - 16:08
Permalink
Worked fine...
Well, I did make a minor change to the script, but it works fine for me.<code>Public Sub saveAttachtoDisk(itm As Outlook.MailItem)Dim objAtt As Outlook.AttachmentDim saveFolder As StringsaveFolder = "C:\temp\"For Each objAtt In itm.AttachmentsobjAtt.SaveAsFile saveFolder & objAtt.DisplayNameSet objAtt = NothingNextEnd Sub</code>.... notice that I left out the & "\" ... if it was left in, the filename would concatenate to c:\temp\\filename.docAdditionally, I did drop the code into ThisOutlookSession, then set up my rule accordingly. It works fine.This is a great little script; does just what it should. There are some enhancements that could be made to it to make it work a little better, such as:1) Check to see if the file exists. If so, try appending (copy) to the filename and check again. You could conceivably get "filename.doc filename (copy).doc and filename (copy) (copy).doc" if you got the same filename 3 times, but they would all be unique names then. Optionally, you could do filename-1.doc filename-2.doc and so forth. Easy enough, just loop through the file.exists until you find one that doesn't exist, then save it.2) Moving the email to another folder after the attachment has been saved; that would show you at a glance if it processed and successfully saved the attachment or not.3) Optionally delete the email after the attachment has been saved, to save your PST/Mailbox sizeI'm going to have to play with this whole idea some more... I have our factory sending over scans of products they make as an email attachment (which is how I found this site), and then they send over the description and specifics in a database file. I think that with some scripting, I could have keywords (almost like XML) in the email body, and write a script that would save the attachment, grab the pseudo-XML in the email body, and save all the information automatically. WOOHOO! No more data entry for me! lolKeep up the great work!
Greg (not verified)
Tue, 09/27/2011 - 16:08
Permalink
However....
However, the formatting of the comment didn't work so well. ;)
Nathan Foreman (not verified)
Thu, 09/29/2011 - 14:29
Permalink
Strait to the point
Script works great and is simple and strait to the point, Thanks for posting it was a great help
Aniela (not verified)
Wed, 10/12/2011 - 09:50
Permalink
Help
I used this with Outlook 2003 and loved it. My company has just upgraded to 2010 (which, so far, I hate) and now I can't get this code to work correctly. What might I be doing wrong?Please help. I scan large quantites of documents which our copier attaches to an email and sends to my desk. The company limits how much we can have in own email account, so I will lose if I can't autosave the attachment to my desktop and auto-delete the email.
northben
Wed, 10/12/2011 - 22:15
Permalink
Your problem might be related
Your problem might be related to Outlook 2010 security settings. Check out this link. Essentially, you will need to:
On the File tab, choose Outlook Options to open the Outlook Options dialog box, and then click Trust Center.
Click Trust Center Settings, and then the Macro Settings option on the left.
Select Notifications for all macros and then click OK. The option allows macros to run in Outlook, but before the macro runs, Outlook prompts you to verify that you want to run the macro.
Restart Outlook for the configuration change to take effect.
Link: http://msdn.microsoft.com/en-us/library/ee814736.aspx
Frank Cork (not verified)
Tue, 10/25/2011 - 02:00
Permalink
Outlook 2010
I have the same issue. company uses Outlook 2010 and cannot get the rule to work even after changing security settings. have tried both in module and Outlooksession.
Frank Cork (not verified)
Tue, 10/25/2011 - 02:04
Permalink
forgot to add
When i point the rule to the scrip i get thisThis rule has a condition that the server cannot process.
Frank Cork (not verified)
Tue, 10/25/2011 - 02:20
Permalink
Found
I found the problem, the example code has back slash after the word Temp and then another one in the loop to save the file which shows as "\\" which what was causing the error. it is the best code i have found for saving the file. nice and basic making it much easier to use as a base for more advance moves.
govind raj (not verified)
Wed, 10/19/2011 - 09:34
Permalink
it not work
i am trying this code its not work . please suport me.
Bill (not verified)
Mon, 10/24/2011 - 10:59
Permalink
Dynamic Folder
This works great. However, is there a method of saving the attachments in dynamically created folders based on date the email was received? Thanks for the help.
Gambith (not verified)
Thu, 10/27/2011 - 12:53
Permalink
Works great
This is exactly what I was looking for. Thank you very much
Dillard (not verified)
Thu, 10/27/2011 - 22:08
Permalink
Adding Date to Filename
It took me a long while tonight to figure this out but for those wanting to add the date to the file name change the following lineobjAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayNameTo objAtt.SaveAsFile saveFolder & "\" & Format$(itm.CreationTime, "yyyymmdd_") & objAtt.DisplayNameThis will append with the date only if you want the time choose thisobjAtt.SaveAsFile saveFolder & "\" & Format$(itm.CreationTime, "yyyymmdd_hhmmss_") & objAtt.DisplayName This uses the Date/Time that from the email creation to set the date.
Mene (not verified)
Sun, 12/18/2011 - 12:53
Permalink
That! comment!
Thank you northben for all the script and Dillard for the option I was looking for! MS Outlook 2007. Worked perfect!
Bill (not verified)
Tue, 11/01/2011 - 10:19
Permalink
File Date
Dillard; Thanks for your submission. That worked fantastic to append the date to the file name!
Pages