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.

  1. Open the VBA IDE in Outlook. Alt-F11 will do this.
  2. 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.
  3. Now, paste the text below in the main VBA window.
  4. Close the VBA IDE.
  5. Create a Rule that calls the script. Tools -> Rules and Alerts -> New Rule...
  6. In the first screen of the new rule wizard, choose "Check messages when they arrive".
  7. 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.
  8. 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.
  9. 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   
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.DisplayName
      end if


If you can't get the rule to work, try adjusting your Outlook security settings:

  1. On the File tab, choose Outlook Options to open the Outlook Options dialog box, and then click Trust Center.

  2. Click Trust Center Settings, and then the Macro Settings option on the left.

  3. 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.

  4. Restart Outlook for the configuration change to take effect.


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.


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.

I moved the code to 'ThisOutlookSession' and it is working now...

I get daily backups from cron and this has saved me hours of filing, thanks!

Esta muy facil y lo mejor de todo "G R A T I S"


This is just awesome.

I haven't created the module under "This Outlook Session". Still, I think its working well.

Thanks for the solution

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!

is there a way to have the file name change - like to the date and time? the attachments all have the same file name.

What a time saver!

It worked and really thankful for your efforts.

can you send me the working script please ?

True legend! this is great

Thank You very much. I have an acquisition system sending daily emails with data. This script saves me a lot of time.

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.

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.

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?


Create a new line just before "Set objAtt = Nothing" and put this code in the new line:


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?)

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.

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!!


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.


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

You're welcome. I'm glad to help out. You don't need to give me any bitcoins though. :)

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

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!


I changed the code to this one:


    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "C:\mans"
        For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
        Set objAtt = Nothing
        End Sub
but sadly none of the attachments show in the folder named "mans".. Is there anything else I can try?

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
End Sub

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
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!!!!


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.


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

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

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.


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.

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.

<p>Hey ben,</p><p>Can't find the link to your vba code !</p><p>Thx,</p><p>Achraf</p>

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.

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


Great Yamouth, England

I  used reply cus the comment field wasn't active on my main screen.

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! 

However, the formatting of the comment didn't work so well. ;) 

Script works great and is simple and strait to the point, Thanks for posting it was a great 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.

Your problem might be related to Outlook 2010 security settings. Check out this link. Essentially, you will need to:

  1. On the File tab, choose Outlook Options to open the Outlook Options dialog box, and then click Trust Center.

  2. Click Trust Center Settings, and then the Macro Settings option on the left.

  3. 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.

  4. Restart Outlook for the configuration change to take effect.



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.

When i point the rule to the scrip i get thisThis rule has a condition that the server cannot process.

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. 

i am trying this code its not work . please suport me.

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.

This is exactly what I was looking for. Thank you very much

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. 

Thank you northben for all the script and Dillard for the option I was looking for! MS Outlook 2007. Worked perfect!

Dillard; Thanks for your submission. That worked fantastic to append the date to the file name!