Tuesday, October 28, 2014

Filled Under:

Outlook rule to auto save attachment


Many people face the problem of saving attachments which are sent on routine basis. For example, I used to receive file daily from our production unit. I had to prepare daily accounts on the basis of production mentioned in that file. It was quite obnoxious for me to daily save the attachment sent in
mail from production unit. This article explains how to auto save attachment by running rule to automatically save attachment.
For this purpose follow the steps given below,
  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".
  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.

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
Dim saveFolder As String
saveFolder = "C:\temp\"
     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
          Set objAtt = Nothing
     Next
End Sub

This will save file with date and time part of file name. This is more applicable where attachment files are of same name daily and you want to retain every file. If files are of different names or you want to replace file with same name; delete the lines

Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")

Then replace the line that begins with 'objAtt.SaveAsFile' with this:

objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName 


If you want to delete attachment from original mail after it has been downloaded to your system, create a new line just before "Set objAtt = Nothing" and put this code in the new line:
objAtt.Delete
 To change default folder where attachment is save, specify folder path in line which begins with saveFolder =

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.