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      
     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.DisplayName
end if

Troubleshooting

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.

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/