Using Outlook rules to control emails with different attachments types

Using Outlook rules to control emails with different attachments types

Outlook is great with rules for many different conditions but often lacking in dealing with attachments. Using VBA for Outlook you can take emails with attachments, rename the files, move based on type, or save the attached files automatically and remove from the email to save on mailbox size.

To illustrate a basic operation of this we have an example of a rule to move any incoming file that has a type of .zip to a junk folder. Why would you want to move .zip files? Emailing files with a type of.zip is an often used phishing attack to encrypt your files.

Follow these instructions to add the code to Outlook.

Sub MoveZipToJunk(Item As Outlook.MailItem)

Dim myAtt As Outlook.Attachment

For Each myAtt In Item.Attachments

If Right(LCase(myAtt.Filename), 4) = ".zip" Then

Item.Move Session.GetDefaultFolder(olFolderJunk)

Exit For

End If

Next

Set myAtt = Nothing

End Sub

  1. Start Outlook
  2. Press ALT + F11 to open the Visual Basic Editor or alternatively this can also be done through the Developer tab.
  3. On the menu bar at the top of the screen, Select Insert → Module.
  4. Copy the code from the code below and paste it into the right-hand pane of Outlook’s VB Editor window
  5. Press Ctrl + S to save the changes
  6. Close the VB Editor (File Menu -> Close and Return to Microsoft Outlook)
Then to create the rule in Outlook and use the script:

  • It is usually the 11th option up from the bottom of the list has attachment
  • It is usually the 4th option up from the bottom of the list run as script
  1. Click Rules -> Create Rule… on the Home tab in the Move group.
  2. Click the button Advanced options…
  3. Set the rule’s condition to which has an attachment.
  4. Click next
  5. Set the rule’s action to run a script and select this script as the one to run.
  6. Click the blue a script in the edit the rule box and select the script from the pop up and click OK:
  7. Finally Click Finish, the rule is now active.

Note: If you have a mobile device, use the web or a second machine to also connect to your Outlook/Exchange account the rules will not work, as the rule will only work on the machine on which you create it and other devices and connections can effectively bypass the rule by processing the email to the inbox therefor bypassing the action that starts the rule.

A walkthrough the code:
The below declares and closes a function/method/subroutine that will accept an Outlook Item, specifically in this case emails.

Sub MoveZipToJunk(Item As Outlook.MailItem)

End Sub
Declares myAtt as a variable that holds an attachment type:

Dim myAtt As Outlook.Attachment
Then a or loop: For Each attachment (an array) in my email item, assign to myAtt:

For Each myAtt In Item.Attachments

Next
The inner code in the for loop:

If the retrieve the file name, for the item, make lowercase (LCase) and then retrieve the 4 characters far right (Right) hand side of the filename. If they are equal to “.zip” Then process inner code.


If Right(LCase(myAtt.Filename), 4) = ".zip" Then



End If
Inner Code:
Move the email (Item) to the default junk folder
Item.Move Session.GetDefaultFolder(olFolderJunk)

Since on the first occurrence of finding a .zip file the item is moved then you no longer need to process any additional attachments exit the for loop.

Exit For
Finally, a bit of garbage clean-up to clean the memory.
Set myAtt = Nothing

Check out our Outlook courses

Download our Outlook cheat sheet

Our experienced instructors have put together some of their most useful keyboard shortcuts into these very easy to use cheat sheets.

Download cheat sheet

Outlook keyboard shortcut cheat sheet

Office Skills Assessments

Free Microsoft PowerPoint Skills Assessment

Short questionnaire to establish your skill level with Microsoft PowerPoint.

PowerPoint Assessment

Top

Download our Cheat Sheets!
Keyboard shortcut cheat sheets for Office and Adobe apps

Our experienced instructors have put together some of their most useful keyboard shortcuts into these very easy to use cheat hseets.

Download now

free resit on Nexus Human courses

Award Winning Trainers

Interact with our experienced and certified trainers during our live online classes.

View our most popular courses

Training insurance, free resit on Nexus Human courses

Free Resit Included

If the situation arises where training delegates fall ill or become unavailable, no need to worry, they can resit the course for free. Our Training Insurance covers you to sit the same public class within 6 months, if the case arises.

View our courses

6 Month Access To Recording

Classes are recorded so that you don't miss a thing. You can go over the class again in order to enforce learning for up to 6 months after the training course has ended.

Instructor was very knowledgeable, was easy to understand and answered all of the questions I came in with.

Accenture

Nexus Human class

Explained very well. So easy to learn from. Very engaging. Broke things down into simple terms. I was so pleasantly surprised by how much learned and how much i enjoyed it.

Nexus Human student

Sharon was very approachable and explained things very well

JC Decaux

Nexus Human Excel class

A brilliant tutor - instruction carried out in understandable lingo and at a pace that could be followed.

Nexus Human student

The patience that Sharon had with everyone was excellent. The pace was also perfect. I would highly recommend this course to my colleagues.

Nexus Human student

Top

Full Course Listing for Microsoft Office

Please click the course title to view outlineDuration
Word for the Business User1 day(s)Enquire now
SharePoint - Site Owner with Power Platform (Modern Experience)1 day(s)Enquire now
SharePoint - Site Basics (Modern Experience)1 day(s)Enquire now
SharePoint - Advanced Site Owner (Modern Experience)1 day(s)Enquire now
Data Analysis and Visualization with Microsoft Excel2 day(s)Enquire now
55301 Mastering Microsoft Project 20193 day(s)Enquire now
Word - Part 21 day(s)Enquire now
Word - Part 11 day(s)Enquire now
55268 Microsoft Power Automate for End Users1 day(s)Enquire now
SharePoint Online Management and Administration3 day(s)Enquire now
Visio1 day(s)Enquire now
Microsoft SharePoint Site User1 day(s)Enquire now
Microsoft Project Level 1 and 22 day(s)Enquire now
Microsoft PowerPoint for the Business User1 day(s)Enquire now
Office 365 SharePoint, OneDrive and Teams1 day(s)Enquire now
Excel - VBA Bootcamp1 day(s)Enquire now
Excel - Excel Functions Including Pivot Tables and Lookups1 day(s)Enquire now
Excel - Excel Essentials and Formulas1 day(s)Enquire now
Excel - Automating Excel with Macros and Analysis Tools1 day(s)Enquire now
Excel - Part 31 day(s)Enquire now
Excel - Part 21 day(s)Enquire now
Excel - Part 11 day(s)Enquire now
Visio - Part 21 day(s)Enquire now
Visio - Part 11 day(s)Enquire now
VBA with Microsoft Excel1 day(s)Enquire now
Project - Part 21 day(s)Enquire now
Project - Part 11 day(s)Enquire now
PowerPoint - Part 21 day(s)Enquire now
PowerPoint - Part 11 day(s)Enquire now
Power BI: Dashboard in a Day1 day(s)Enquire now
Outlook - Part 21 day(s)Enquire now
Outlook - Part 11 day(s)Enquire now
Outlook - Find & Search0.125 day(s)Enquire now
Outlook All about Mail Merge0.125 day(s)Enquire now
OneNote1 day(s)Enquire now
Microsoft SharePoint 2016 - Site Owner1 day(s)Enquire now
Microsoft Power BI - Data Analysis Practitioner2 day(s)Enquire now
Word - Part 31 day(s)Enquire now
Microsoft Office Publisher 2016/20191 day(s)Enquire now
Microsoft Office 365 Online (with Teams for the Desktop)1 day(s)Enquire now
Excel - Tables Pivot Tables and Conditional Formatting0.5 day(s)Enquire now
Excel - Data Analysis with Power Pivot0.5 day(s)Enquire now
Excel - Data Analysis with PivotTables0.5 day(s)Enquire now
Access - Part 22 day(s)Enquire now
Access - Part 12 day(s)Enquire now
55238 SharePoint Online for Administrators3 day(s)Enquire now
55215 SharePoint Online Power User4 day(s)Enquire now
MS Project (Part 1) - Project Initiation and Planning1 day(s)Enquire now
Microsoft Word for Office 365 (Desktop or Online) (v1.1) Part 11 day(s)Enquire now
Microsoft PowerPoint for Office 365 (Desktop or Online) - Part 1 ( v1.1)1 day(s)Enquire now
Microsoft Outlook for Office 365 (Desktop or Online) - Part 11 day(s)Enquire now
Microsoft Office Word 2021: Part 11 day(s)Enquire now
MS Project (Part 2) - Project Validation, Baseline and Tracking using Microsoft Project1 day(s)Enquire now
Microsoft Word for Office 365 (Desktop or Online) (v1.1) Part 21 day(s)Enquire now
Microsoft PowerPoint for Office 365 (Desktop or Online) - Part 2 ( v1.1)1 day(s)Enquire now
Microsoft Outlook for Office 365 (Desktop or Online) - Part 21 day(s)Enquire now
Microsoft Office Word 2021: Part 21 day(s)Enquire now
Microsoft Word for Office 365 (Desktop or Online) (v1.1) Part 31 day(s)Enquire now
Microsoft Office Word 2021: Part 31 day(s)Enquire now
Microsoft 365 Office for the Web Productivity Apps v2.01 day(s)Enquire now
Office 365 Web Apps (with Skype for Business)1 day(s)Enquire now
Microsoft SharePoint 2016 - Site User1 day(s)Enquire now
Microsoft SharePoint 2016 - Advanced Site Owner with Workflow Administration1 day(s)Enquire now
Office 365 - Essentials1 day(s)Enquire now
Office 365 - An Overview1 day(s)Enquire now
Microsoft 365 Office for the Web (with Teams)1 day(s)Enquire now
Microsoft Teams for the Remote Business User0.5 day(s)Enquire now
Formatting Technical Documents Using Microsoft for Office 3652 day(s)Enquire now
Excel for business professionals2.25 day(s)Enquire now
Office365 Teams, SharePoint and One Drive1 day(s)Enquire now
Microsoft SharePoint Modern Experience: Site Owner with Power Platform1 day(s)Enquire now
Microsoft Project3 day(s)Enquire now
Microsoft SharePoint Modern Experience: Advanced Site Owner1 day(s)Enquire now
Microsoft 365 for End-users2 day(s)Enquire now
55234 SharePoint 2016 Site Collections and Site Owner Administration5 day(s)Enquire now
55355 SharePoint 2016 Administration5 day(s)Enquire now
Microsoft 365 for the Web (with Teams, SharePoint, and OneDrive) v3.01 day(s)Enquire now