You can use a Run a Script rule or an ItemAdd macro to look for messages with attachments, then check the file type. If certain file types are found, the macro does something with the message, in the example, the message is moved to a folder.
Use a run a script rule if your mail load is lighter and you want to use the script only on messages meeting certain conditions. Use an ItemAdd macro for heavier mail volumes or when you need to check all messages you receive.
Use a Run a Script rule
To use the run a script rule, create a rule.
Select the has an attachment condition and any other conditions you want to check. The only action will be run a script. Set exceptions as needed.
If you need to do more actions, such as mark the message read, forward a copy, etc, you'll need to add it to the script.
To check additional file types, use 4 characters for the extension name. If the extension is 3 characters, use the leading dot in the Select Case list: docx or .doc
Run a script macros will work in ThisOutlookSession but should be placed in a module.
Sub MoveMail(Item As Outlook.MailItem) If Item.Attachments.Count > 0 Then Dim attCount As Long Dim strFile As String Dim sFileType As String attCount = Item.Attachments.Count For i = attCount To 1 Step -1 strFile = Item.Attachments.Item(i).FileName sFileType = LCase$(Right$(strFile, 4)) Select Case sFileType Case ".pdf", ".doc", "docx" ' do something if the file types are found ' this code moves the message Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Move")) ' stop checking if a match is found and exit sub GoTo endsub End Select Next i End If endsub: Set Item = Nothing End Sub
Use an ItemAdd macro
ItemAdd macros can handle larger volumes of mail and can watch any folder for new items. This macro must be placed in ThisOutlookSession. Click in Application_Startup to test the macro without restarting Outlook.
Option Explicit Private WithEvents olInboxItems As Items Private Sub Application_Startup() Dim objNS As NameSpace Set objNS = Application.Session ' instantiate objects declared WithEvents Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items Set objNS = Nothing End Sub Private Sub olInboxItems_ItemAdd(ByVal Item As Object) On Error Resume Next If Item.Attachments.Count > 0 Then Dim attCount As Long Dim strFile As String Dim sFileType As String Dim i attCount = Item.Attachments.Count For i = attCount To 1 Step -1 strFile = Item.Attachments.Item(i).FileName Debug.Print strFile sFileType = LCase$(Right$(strFile, 4)) Debug.Print sFileType Select Case sFileType Case ".pdf", ".doc", "docx" ' do something if the file types are found ' this code moves the message Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Move")) Exit Sub End Select Next i End If End Sub
How to use these macros
First: You need to have macro security set to low during testing. The macros will not work otherwise.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Some macros need to be in ThisOutlookSession, others go into a module or can be placed in either ThisOutlookSession or a module. The instructions are below.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
If you are told to put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
If you are told to put the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
More information as well as screenshots are at How to use the VBA Editor
More Run a Script Samples:
- Autoaccept a Meeting Request using Rules
- Automatically Add a Category to Accepted Meetings
- Blocking Mail From New Top-Level Domains
- Convert RTF Messages to Plain Text Format
- Create a rule to delete mail after a number of days
- Create a Task from an Email using a Rule
- Create an Outlook Appointment from a Message
- Create Appointment From Email Automatically
- Delegates, Meeting Requests, and Rules
- Delete attachments from messages
- Forward meeting details to another address
- How to Change the Font used for Outlook's RSS Feeds
- How to Process Mail After Business Hours
- Keep Canceled Meetings on Outlook's Calendar
- Macro to Print Outlook email attachments as they arrive
- Move messages CC'd to an address
- Open All Hyperlinks in an Outlook Email Message
- Outlook AutoReplies: One Script, Many Responses
- Outlook's Rules and Alerts: Run a Script
- Process messages received on a day of the week
- Read Outlook Messages using Plain Text
- Receive a Reminder When a Message Doesn't Arrive?
- Run a script rule: Autoreply using a template
- Run a script rule: Reply to a message
- Run a Script Rule: Send a New Message when a Message Arrives
- Run Rules Now using a Macro
- Run-a-Script Rules Missing in Outlook
- Save all incoming messages to the hard drive
- Save and Rename Outlook Email Attachments
- Save Attachments to the Hard Drive
- Save Outlook Email as a PDF
- Sort messages by Sender domain
- Talking Reminders
- To create a rule with wildcards
- Use a Macro to Copy Data in an Email to Excel
- Use a Rule to delete older messages as new ones arrive
- Use a run a script rule to mark messages read
- Use VBA to move messages with attachments