We run you through all the steps required
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
- Start Outlook
- Press ALT + F11 to open the Visual Basic Editor or alternatively this can also be done through the Developer tab.
- On the menu bar at the top of the screen, Select Insert → Module.
- Copy the code from the code below and paste it into the right-hand pane of Outlook’s VB Editor window
- Press Ctrl + S to save the changes
- 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 
- Click Rules -> Create Rule… on the Home tab in the Move group.
- Click the button Advanced options…
- Set the rule’s condition to which has an attachment.
- Click next
- Set the rule’s action to run a script and select this script as the one to run.
- Click the blue a script in the edit the rule box and select the script from the pop up and click OK:
- 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
You may be interested in our Outlook courses
Outlook 2016 one day courses
Outlook 2013 one day courses
Outlook 2010 one day courses
View all out Outlook courses here