Enable or disable an Outlook rule using reminders and VBA

Last reviewed on May 22, 2013

Applies to: Microsoft Outlook 2010, Outlook 2007

The ability to use VBA to control Outlook's rules was added to Outlook 2007. For this reason, this macro will not work with Outlook 2003 or older.

Enter the name of the rule in the VBA codeIn the enable and disable macro, replace "Your Rule Name" with the name of the rule you want to enable and disable.

Set olRule = olRules.Item("Your Rule Name")

To use: Create a task with the subject "Disable Rule" and set the reminder time for the time you want the rule to be disabled. Create a second task with the subject "Enable Rule" with the reminder time set for the time you want it enabled.

This, of course, only works if Outlook is running. It is not possible to enable and disable rules when Outlook is closed.

VBA code sample to enable or disable rules

Use Alt+F11 to open the VBA Editor and place this code in ThisOutlookSession.

If you prefer to use Appointments instead of Tasks, replace IPM.Task with IPM.Appointment. If you are using recurring events to turn the rule off and on, an appointment might be better as the reminder is less likely to be accidentally disabled.

Private Sub Application_Reminder(ByVal Item As Object)

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

If Item.Subject = "Enable Rule" Then
  Enable_Run_Rule
End If

If Item.Subject = "Disable Rule" Then
 Disable_Run_Rule
End If

End Sub


' Macro to enable a rule
Sub Enable_Run_Rule()
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim intCount As Integer
Dim blnExecute As Boolean
 
    Set olRules = Application.Session.DefaultStore.GetRules
    Set olRule = olRules.Item("Your Rule Name")
    olRule.Enabled = True
    If blnExecute Then olRule.Execute ShowProgress:=True
     olRules.Save
  
    Set olRules = Nothing
    Set olRule = Nothing
End Sub

' Macro to disable a rule
Sub Disable_Run_Rule()
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim intCount As Integer
Dim blnExecute As Boolean
 
    Set olRules = Application.Session.DefaultStore.GetRules
    Set olRule = olRules.Item("Your Rule Name")
    olRule.Enabled = False
    If blnExecute Then olRule.Execute ShowProgress:=True
       olRules.Save
  
    Set olRules = Nothing
    Set olRule = Nothing
End Sub


Use with multiple rules

To apply this to multiple rules, assign categories called Enable Rule or Disable Rule to a task and use the rule name as the task subject. Use an If... then statement in the VBA to check for the category and pass the rule name to a function.

In the following code, I condensed it to one function that gets the rule name and whether the rule should be enabled or disabled, passing both values to the function.


Private Sub Application_Reminder(ByVal Item As Object)
Dim strRule As String

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

strRule = Item.Subject

If Item.Categories = "Enable Rule" Then
Run_Rule strRule, 1
End If

If Item.Categories = "Disable Rule" Then
Run_Rule strRule, 0
End If

End Sub

Function Run_Rule(rule_name As String, tf As Boolean) As Boolean
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim blnExecute As Boolean

    Set olRules = Application.Session.DefaultStore.GetRules
    Set olRule = olRules.Item(rule_name)
    
    olRule.Enabled = tf
    
    If blnExecute Then olRule.Execute ShowProgress:=True
     olRules.Save
  
    Set olRules = Nothing
    Set olRule = Nothing
End Function

Toggle the rule on or off

If you want to use one macro and toggle the rule on or off, you can change olRule.Enabled = True to

If olRule.Enabled = True then
    olRule.Enabled = False
Else 
    olRule.Enabled = True

More Information

Enable / Disable rules code sample from Outlook Code.com

Written by

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.

If the Post Coment button disappears, press your Tab key.