Using Outlook rules to control emails with different attachments types

Blog..

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
  • It is usually the 4th option up from the bottom of the list
  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