Rule to automatically save attachment in Outlook
- 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. - Change the saveFolder option to a folder of your choosing.
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
- 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 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
Having Problems? Try adjusting your Outlook security settings:
- On the Filetab, choose Outlook Options to open the Outlook Options dialog box, and then click Trust Center.
- Click Trust Center Settings, and then the Macro Settingsoption on the left.
- Select Notifications for all macrosand 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.
Not seeing the "Run A Script" Option in the rules?
To restore the missing run a script option in the Outlook rules wizard, please do as follows:
- 1. Press Win+ Rkeys together to open the Run dialog box, in the dialog box type regedit in the Open box, and click the OK See screenshot:
- 2. In the popping out User Account Control dialog box, please click the Yes See screenshot:
- 3. In the Registry Editor window, please open the Securitykey with below path:
HKEY_CURRENT_USERSoftwareMicrosoftOffice16.0OutlookSecurity
- 4. Right click the Securitykey, and select New> DWORD (32-BIT Value) from the right-clicking menu, and then name the new DWORD as EnableUnsafeClientMailRules. See screenshot:
- 5. Double click the EnableUnsafeClientMailRulesDWORD to open the Edit QWORD Value dialog box. See screenshot:
- 6. In the Edit QWORD (64-BIT) Value dialog box, type 1in the Value databox, and click the OK See screenshot above.
- 7. Restart your Microsoft Outlook.
And now you will find out the run a script option in the Step 1: Select action (s) of Rules Wizard.
How can i extract attachments to a folder automatically in Microsoft Outlook 2016?
Rule to automatically save attachment in Outlook
- 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. - Change the saveFolder option to a folder of your choosing.
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
- 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 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
Having Problems? Try adjusting your Outlook security settings:
- On the Filetab, choose Outlook Options to open the Outlook Options dialog box, and then click Trust Center.
- Click Trust Center Settings, and then the Macro Settingsoption on the left.
- Select Notifications for all macrosand 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.
Not seeing the "Run A Script" Option in the rules?
To restore the missing run a script option in the Outlook rules wizard, please do as follows:
- 1. Press Win+ Rkeys together to open the Run dialog box, in the dialog box type regedit in the Open box, and click the OK See screenshot:
- 2. In the popping out User Account Control dialog box, please click the Yes See screenshot:
- 3. In the Registry Editor window, please open the Securitykey with below path:
HKEY_CURRENT_USERSoftwareMicrosoftOffice16.0OutlookSecurity
- 4. Right click the Securitykey, and select New> DWORD (32-BIT Value) from the right-clicking menu, and then name the new DWORD as EnableUnsafeClientMailRules. See screenshot:
- 5. Double click the EnableUnsafeClientMailRulesDWORD to open the Edit QWORD Value dialog box. See screenshot:
- 6. In the Edit QWORD (64-BIT) Value dialog box, type 1in the Value databox, and click the OK See screenshot above.
- 7. Restart your Microsoft Outlook.
And now you will find out the run a script option in the Step 1: Select action (s) of Rules Wizard.