Applying message rules to the Junk E-mail folder

Junk E-mail buttonWhen you’ve created a rule to directly delete the most obvious spam messages, rather than letting them go to the Junk E-mail folder, you might have noticed that this method isn’t always successful.

This is because once a message has been marked as Junk via the Junk E-mail Filter, no rules will be executed for that message. The reasoning behind this is to prevent it from being moved out of the folder again or to have any other undesirable action being executed because of it (like auto-replying to it).

While you could still manually execute your anti-Junk rules against the Junk E-mail folder to speed up your reviewing process, it is a quite cumbersome process to get to the option and select the rules to execute.

With the VBA macro from this guide, you can execute all your anti-Junk E-mail rules against your Junk E-mail folder with a single click.


Run rules manually

Manage Rules & Alerts buttonBefore getting to the automated VBA solution, let’s quickly look at what it takes to do it manually.

  1. Open the “Manage Rules & Alerts” dialog
    • File-> Manage Rules and Alerts
  2. Click on the button: Run Rules Now…
  3. Locate and select your anti-Junk rules.
  4. Select the Junk E-mail folder.
  5. Click Run Now.

Even if you have only 1 anti-Junk rule and are already in the Junk E-mail folder, that is still at least 6 clicks and 3 different screens/dialogs you have to deal with. So Junk E-mail is still wasting your time!

Running your own Junk Filtering rules is quite cumbersome.
Running your own Junk Filtering rules is quite cumbersome.

RunAllJunkRules VBA macro

Visual Basic buttonThe RunAllJunkRules macro automates the above process and will run all rules which have the word “Junk” in them (case insensitive).

There is not even a need to select your Junk E-mail folder first or to have the rules enabled in your list of rules (which means that it won’t count for your Exchange rule limit either).

Outlook will let you know when it is done executing the rules against your Junk E-mail folder and will display an overview of the executed rules.

Quickly clean up your Junk E-mail with the RunAllJunkRules macro.
Quickly clean up your Junk E-mail with the RunAllJunkRules macro.

Quick Install

Use the following instructions to configure the macro in Outlook;

  1. Download this code-file (runalljunkrules.zip) or copy the code below.
  2. Open the VBA Editor (keyboard shortcut ALT+F11).
  3. Extract the zip-file and import the RunAllJunkRules.bas file via File-> Import…
    If you copied the code, paste it into a new module.
  4. Sign your code so you won’t get any security prompts and the macro won’t get disabled.
  5. Add a button for easy access to the macro or press ALT+F8 and select the macro you want to execute.

Add a button to the Ribbon or the QAT to run all your Junk E-mail rules.
Add a button to the Ribbon or the QAT to run all your Junk E-mail rules.

Macro Code

The following code is contained in the zip-file referenced in the Quick Install. You can use the code below for review or manual installation.

Sub RunAllJunkRules()

'=================================================================
'Description: Outlook macro to run all message rules with
'              "Junk" in their name against the Junk E-mail folder
'
' author : Robert Sparnaaij
' version: 1.0
' website: https://www.howto-outlook.com/howto/junkrules.htm
'=================================================================

    Dim st As Outlook.Store
    Dim myRules As Outlook.Rules
    Dim rl As Outlook.Rule
    Dim ruleList As String
    Dim fldJunk As Outlook.Folder
    'On Error Resume Next
     
    ' get default store (where rules live)
    Set st = Application.Session.DefaultStore
    ' get rules
    Set myRules = st.GetRules
    ' get Junk e-mail folder
    Set fldJunk = st.GetDefaultFolder(olFolderJunk)
     
    ' iterate all the rules
    For Each rl In myRules
        ' determine if it's an Inbox rule
        If rl.RuleType = olRuleReceive Then
            ' determine if it is a Junk rule
            If InStr(LCase(rl.Name), "junk") > 0 Then
            ' if so, run it against the Junk E-mail folder
                rl.Execute ShowProgress:=True, Folder:=fldJunk
                ruleList = ruleList & vbCrLf & rl.Name
            End If
        End If
    Next
     
    ' tell the user what you did
    ruleList = "These rules were executed against the " & _
          "Junk E-mail folder: " & vbCrLf & ruleList
    MsgBox ruleList, vbInformation, "Macro: RunAllJunkRules"
     
    Set rl = Nothing
    Set st = Nothing
    Set myRules = Nothing
    Set fldJunk = Nothing

End Sub