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
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':
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') ThenobjAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayNameend if
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.