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