• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Run Rules Now using a Macro

Slipstick Systems

› Outlook › Rules, Filters & Views › Run Rules Now using a Macro

Last reviewed on December 4, 2018     47 Comments

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.

Eric had a request to run rules based on a condition, in his case, when a message with a specific subject arrives.

If I send myself a message with a specific subject line I want outlook to execute a set of predefined rules already in my outlook client.

Sure, you can do this using a Run a Script rule.

  1. Create the rules you want to run but do not enable them to run automatically.
  2. Paste the RunRules macro below into a new module in Outlook's VB Editor.
  3. Type your rule names into the olRuleNames array
  4. Create a Rule that looks for a message with your specific subject line. Select Run a Script as the Action and select the RunRules macro.
    run rules now using a macro
  5. Send yourself a test message.

Sub RunRules(Item As Outlook.MailItem)

Dim olRules As Outlook.Rules
Dim myRule As Outlook.Rule
Dim olRuleNames() As Variant
Dim name As Variant

' Enter the names of the rules you want to run       
olRuleNames = Array("SetCategory", "SetFlag", "MoveClutter")

Set olRules = Application.Session.DefaultStore.GetRules()

For Each name In olRuleNames()
    For Each myRule In olRules
    ' Rules we want to run
        If myRule.name = name Then
        myRule.Execute ShowProgress:=True
        End If
    Next
Next
End Sub

myRule.Execute supports 4 optional parameters. These are only needed if you don't want to use the defaults.

myRule.Execute ShowProgress:=True, Folder:=Session.GetDefaultFolder(olFolderInbox).Folders("my subfolder"), IncludeSubfolders:=True, RuleExecuteOption:=1

ParameterPossible Values
ShowProgressTrue or False
Use to display the Progress dialog normally seen when using Run Rules Now command. Default is False.
FolderFolder path
Use if you want to run the rules on a folder other than the default Inbox. See "Working with VBA and non-default Outlook Folders". A macro below runs rules in a secondary account.
IncludeSubfoldersTrue or False
Use if you want to run the rules on subfolders. Default is False.
RuleExecuteOptionolRuleExecuteAllMessages or 0
olRuleExecuteReadMessages or 1
olRuleExecuteUnreadMessages or 2
Use to limit the rules to either read messages, unread messages, or apply to all messages. Default is to run the rules on all messages. You can use either the property name or the number.

Run Rules when a Task Reminder Fires

If you prefer to run the rules on a schedule, use a task reminder to trigger the RunRules macro. Set up a recurring task called Run Rules and set a reminder. When the task reminder comes up, it triggers the RunRules macro.

This version of the RunRules macro can be triggered using a button on the ribbon or QAT

Private Sub Application_Reminder(ByVal Item As Object)
 
If Item.MessageClass <> "IPM.Task" Then
  Exit Sub
End If
 
If Item.Subject = "Run Rules" Then
  RunRules
End If

End Sub

Sub RunRules()

Dim olRules As Outlook.Rules
Dim myRule As Outlook.Rule
Dim olRuleNames() As Variant
Dim name As Variant
        
olRuleNames = Array("SetCategory", "SetFlag", "MoveClutter")

Set olRules = Application.Session.DefaultStore.GetRules()

For Each name In olRuleNames()
    For Each myRule In olRules
    ' Rules we want to run
        If myRule.name = name Then
        myRule.Execute ShowProgress:=True
        End If
    Next
Next
End Sub

 

Run Rules on a Secondary Inbox

If you want to run rules in a secondary account, you need to find the account using Outlook.Stores and add the Folder parameter. Because the folder is not in the default data file, you need to use GetFolderPath function.

Tip: Use the Folder parameter to run it on any folder. See "Working with VBA and non-default Outlook Folders" for more information on how to identify folders.

As written, this macro only runs when you run it. You can add it a ribbon button for easier access.


Sub RunRulesSecondary()

Dim oStores As Outlook.Stores
Dim oStore As Outlook.Store
     
Dim olRules As Outlook.Rules
Dim myRule As Outlook.Rule
Dim olRuleNames() As Variant
Dim name As Variant

' Enter the names of the rules you want to run
olRuleNames = Array("Rule 1 Name", "Rule 2 Name", "Rule 3 Name")

Set oStores = Application.Session.Stores
For Each oStore In oStores
On Error Resume Next

' use the display name as it appears in the navigation pane
If oStore.DisplayName = "alias@domain.com" Then

Set olRules = oStore.GetRules()
 
For Each name In olRuleNames()

    For Each myRule In olRules
       Debug.Print "myrule " & myRule
       
     If myRule.name = name Then

' inbox belonging to oStore
' need GetfolderPath functionhttp://slipstick.me/4eb2l
        myRule.Execute ShowProgress:=True, Folder:=GetFolderPath(oStore.DisplayName & "\Inbox")  

' current folder
'      myRule.Execute ShowProgress:=True, Folder:=Application.ActiveExplorer.CurrentFolder

       End If
    Next
Next

End If
Next
End Sub

How to use macros

First: You will need macro security set to low during testing.

To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

More information as well as screenshots are at How to use the VBA Editor

More Information

Enable or disable an Outlook rule using reminders and VBA

Rule.Execute Method (Outlook)

Run Rules Now using a Macro was last modified: December 4th, 2018 by Diane Poremsky

Related Posts:

  • Enable or Disable an Outlook Rule using Reminders
  • Run all Outlook Rules on Startup
  • Create a List of Rules
  • Server-side vs. Client-side Rules

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
47 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Shyam
August 13, 2021 2:25 pm

Can the rules with vba script setup at microsoft exchange or server level. With the emails in seen in phone, client only doesn't help many times.

0
0
Reply
Imran
August 12, 2021 7:32 am

Great mam. thanks for this macro which helped me running my stuck rules.

0
0
Reply
Dennis Durnian
October 27, 2020 10:42 am

I am having difficulty in making the macro work. Have copied the code you have given into a new module with different rule names. Also created a rule which specifying the subject text and put RunScript as action but on clicking the it a box appears to choose the script but it has no script listed. What am i doing wrong please?

0
0
Reply
Raghu
February 25, 2020 8:06 pm

I am new to Outlook VBA stuff. I made a little tweak to run 'all' my rules when the task reminder pops up.
but I noticed it's taking too long..
Is there a way to restrict the rule to run only on the items received in the last 24 hours, I googled around this and found something like items.restrict() but cannot get this implemented successfully. any ideas here please ?

0
0
Reply
Diane Poremsky
Author
Reply to  Raghu
February 25, 2020 10:24 pm

The problem with restrict() is that it won't work with the rules. There is a date condition in rules, but that would need changed every time.

What are you doing with the rules and how many rules are there? It might be better to use a macro to do everything rather than a rule - that would support restrict.

0
0
Reply
Raghu
Reply to  Diane Poremsky
February 26, 2020 2:01 pm

I have near to 10 rules. They move emails (alerts about infrastructure health) from different systems/servers to different folders. I don't care about all the emails/alerts that arrive to my mailbox when I am off-shift and when I am on-shift - I want everything to be in my Inbox so that I don't miss any when looking from other devices like IPhone..etc. So, I usually hit the button 'Run All rules' in outlook when I reach office and then leave it for about 5 mins to clear all the junk (alerts that came in when I am off shift). Now - I am using this code you have helped us with, it works good but takes a little bit of time. Not a big problem, I just scheduled this task to run 30 mins before my login time.. So, I am very happy with the solution provided in the website.. Thank you so much. but just curious - if there is a way we can just run the rules over the items received in the last 24 hours (retrieving the current date from system and just run over the items received in the last 24 hours), just to make the… Read more »

0
0
Reply
Diane Poremsky
Author
Reply to  Raghu
February 26, 2020 11:55 pm

Not using Rules. They will run on everything in the folder. A regular macro that moves or deletes by subject would work though.

0
0
Reply
Roger Bertrand
June 24, 2019 2:27 am

I am just looking for the exact code to run a macro that will run all rules on a specific folder or the current folder... If you can help thanks.

0
0
Reply
Raghu
Reply to  Roger Bertrand
February 25, 2020 8:13 pm

This worked for me... removed couple of lines from the original code. But it appears to be very slow as it (all rules) runs against all items...
#####
Private Sub Application_Reminder(ByVal Item As Object)

If Item.MessageClass "IPM.Task" Then
Exit Sub
End If

If Item.Subject = "Run_Rules" Then
RunRules
End If

End Sub

Sub RunRules()

Dim olRules As Outlook.Rules
Dim myRule As Outlook.Rule
Set olRules = Application.Session.DefaultStore.GetRules()

For Each myRule In olRules
' Rules we want to run

myRule.Execute ShowProgress:=True

Next

End Sub
#####

0
0
Reply
Mangesh
January 21, 2019 1:27 am

I am working on one task where I want outlook to open an Application(Also passing the input to it) when users opens a mail message to read. Could you help me achieve it.

0
0
Reply
fred
November 28, 2018 6:35 am

Well done!! works perfectly!

0
0
Reply
Kauê
August 24, 2018 10:05 am

Hi Diane,
Would you help me?
I'm having a serious problem and I can not solve ...
Here at the company where I work, we use Outlook 2010.

I have created a rule that is executed as follows: Checks if the received email has "@ example.com.br" or "@ example2.com.br", if yes, it categorizes the email as "Consultant Losango", if not, it does nothing.

The rule runs manually when I select to run ...

However, the rule only executes in the user's inbox, in the shared mail box we use (name: "socialcommerce@example.com") does not execute.
This shared email is used by about 20 people, the rules do not run automatically on generic emails.

I figured I'd have a way to create a macro that would execute the rule only on the generic key "socialcommerce@example.com" whenever a new message arrived in that box.

There must be a way? I have tried several codes in different ways and I could not.

0
0
Reply
Diane Poremsky
Author
Reply to  Kauê
August 24, 2018 11:29 pm

Rules doesn't run on mailboxes opened as shared mailboxes - they only work in accounts. Is the rule listed as 'client side' ? If so, you'll need to use a macro. If its a server side rule, you need to create a profile with only the shared mailbox in it and set up the rule. A server side rule doesn't rely on the account being open in Outlook, so it will work. Instructions are here: https://www.slipstick.com/exchange/create-rules-and-oof-shared-mailbox/

if you need to use a macro, you'll need to use an item add macro and configure it to watch the shared mailbox. An example of a macro is here:
https://www.slipstick.com/developer/code-samples/use-macro-assign-messages-shared-mailbox/

1
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 36

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • Reset the New Outlook Profile
  • This operation has been cancelled due to restrictions
  • Disable "Always ask before opening" Dialog
  • Adjusting Outlook's Zoom Setting in Email
  • How to Hide or Delete Outlook's Default Folders
  • Removing Suggested Accounts in New Outlook
  • Remove a password from an Outlook *.pst File
  • Syncing Outlook with an Android smartphone
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

:wpds_smile::wpds_grin::wpds_wink::wpds_mrgreen::wpds_neutral::wpds_twisted::wpds_arrow::wpds_shock::wpds_unamused::wpds_cool::wpds_evil::wpds_oops::wpds_razz::wpds_roll::wpds_cry::wpds_eek::wpds_lol::wpds_mad::wpds_sad::wpds_exclamation::wpds_question::wpds_idea::wpds_hmm::wpds_beg::wpds_whew::wpds_chuckle::wpds_silly::wpds_envy::wpds_shutmouth:
wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment