VB 196
Excel VBA Auto Save/Store Outlook Attachments By mike on 28th March 2021 03:54:24 PM
  1. Save attachments of new incoming mail automatically to a folder
  2.  
  3.  
  4. ######### PREREQUISITE #########
  5. For outlook v14 / v15 / v16 or newer, you need to set the EnableUnsafeClientMailRules value in the registry then restart Outlook.
  6. Source: https://www.slipstick.com/outlook/rules/outlook-run-a-script-rules/
  7.  
  8. Outlook 2016
  9. HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Security
  10. DWORD: EnableUnsafeClientMailRules
  11. Value: 1
  12.  
  13. Outlook 2013
  14. HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Security
  15. DWORD: EnableUnsafeClientMailRules
  16. Value: 1
  17.  
  18. Outlook 2010
  19. HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Security
  20. DWORD: EnableUnsafeClientMailRules
  21. Value: 1
  22.  
  23.  
  24. ######### VBA #########
  25. Source: https://www.extendoffice.com/documents/outlook/3747-outlook-auto-download-save-attachments-to-folder.html
  26. Add following VBA as a module in outlook.
  27.  
  28. Remember that "Microsoft Scripting Runtime", "Microsoft Office 16.0 Object Library" and "Microsoft Outlook 16.0 Object Library" need to be enabled in
  29. VBA-Editor --> Tools --> References.
  30.  
  31. Furthermore, Outlook needs to be set to allow to run macro's in
  32. Outlook --> Options --> Trust --> Trust Settings --> Macro Settings
  33.  
  34. Anyway, here the code ...
  35. Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
  36. Dim oAttachment As Outlook.Attachment
  37. Dim sSaveFolder As String
  38. sSaveFolder = "C:\TMP\my-folder-with-a-trailing-backslash\"
  39. For Each oAttachment In MItem.Attachments
  40. 'oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
  41. oAttachment.SaveAsFile sSaveFolder & Format(MItem.ReceivedTime, "yyyy-mm-dd hhmmss") & " - " & oAttachment.DisplayName
  42. Next
  43. End Sub
  44.  
  45.  
  46. ######### Set Up Mail Rules #########
  47. Set up mail rules for (new) applicable mail as per normal, and select "run a script" [[now available due to registry edit. The option is more or less between the options 'mark it as read' and 'stop processing more rules']]
  48.  
  49. This will do the trick. You can test it to manually run your mail rule on (a) certain (sub)folder(s).
  50.  
  51.  
  52. ######### Famous last words #########
  53. And as confusing it is every time;
  54.  
  55. Outlook rules daisy chain, in a cascade, so each email is processed against multiple rules. To answer your question, the "Stop Processing rules" step applies only to the current mail, which meets the required criteria.
  56.  
  57. Any subsequent rule that might match that email will not be evaluated. that’s primarily what the message is telling you. The order of rules is important, and its logical that no rule can do anything more with an email once its been deleted, so once the rule is matched, it must either be the last one in the list, or the rules after it have to be skipped.
  58.  
  59. All the rules still remain active though, and when a new email comes in, they will process it until either reaching the last rule, or hitting a stop.
  60.  
  61. ---
  62.  
  63. if you're using Exchange or Office 365), like sieve.
  64. This is really handy if you check your mail on your mobile app, or the outlook website, as they don't process rules at all.
  65.  
  66. Unfortunately though, unlike sieve, it isn't super capable, and a lot of conditions generate a once-off warning saying this rule has a condition that the server cannot process, meaning, this rule will only work whenever you open Outlook on your PC
  67.  
  68. But now this is where this warning kicks in. You've also asked it to stop processing more rules, remember? We know the server cannot run this rule: So how would the server know whether to run following rules?
  69. It doesn't and wont.
  70.  
  71. Basically it's trying to warn you that none of the rules after this one will be ran at all on the server, even if they could.
  72. This mightn't affect you, so you may ignore it, but is a pretty important message if you:
  73.  
  74. have auto reply rules or similar that need to happen immediately in response to emails and don't have your PC with outlook running 24/7
  75. use mobile devices to do quick checks of your email, but receive a tonne of mail and rely on filtering into subfolders/deletion for these checks to actually be quick
  76. or use browsers as the mail client more often than not

Hasta la pasta! is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.