Last reviewed on December 30, 2014   —  32 Comments

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
Run Rules Now using a Macro
VBA to Sort Rules [A-Z] - code provided

Comments

  1. Esben Rasmussen says

    Awesome! This is just what I needed!

    But just to be clear: Since it is based on the reminder, your Outlook doesn't have to be opened at the time the rule should be turned off?

    E.g. if you set the reminder to 8 AM but you are late and Outlook is first started at 8.30 AM the reminder will still activate and therefore the rule will still be turned off - just later than planned?

    • Diane Poremsky says

      Because it is reminder-based, Outlook must be open for this to work. But yes, if you open Outlook after the reminder was supposed to fire, the macro will run when the reminder fires after-the-fact.

  2. Esben Rasmussen says

    Ok, just tried to use it but I get an error "The attempted operation failed. An object could not be found."

    When debugging the line "Set olRule = olRules.Item("My rule")" is highlighted.

    I suspect this is due to the fact that I have two mailboxes I monitor: Mailbox X (my default) and mailbox Y.

    I created the needed rule for mailbox Y but seeing as the macro fails could it be that it only tries to use the rules defined for the default mailbox?

    • Diane Poremsky says

      The macro is written for the rules in the default mailbox: Set olRules = Application.Session.DefaultStore.GetRules . That would need changed to apply to other stores.

  3. Esben Rasmussen says

    Probably not the most elegant solution but here is what I did to get i working on another mailbox than my defailt (the applied rule is unique for that mailbox).

    ' 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
    Dim colStores As Outlook.Stores
    Dim oStore As Outlook.Store

    On Error Resume Next
    Set colStores = Application.Session.Stores
    For Each oStore In colStores
    Set olRules = oStore.GetRules
    Set olRule = olRules.Item("My rule")
    olRule.Enabled = True
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save
    Set olRules = Nothing
    Set olRule = Nothing
    Next

    End Sub

    The macro to disable the rule is exactly the same - except "olRule.Enabled = False".

    • Diane Poremsky says

      Cool, thanks for sharing. Some of my macros aren't the most elegant but they get the job done. :)

  4. Dave Mack says

    Seems pretty useless if it does not work when Outlook is closed. My company enforces everyone logging off every night before leaving.

    • Diane Poremsky says

      Yeah, you'd need to run it before you log off. It would be possible to run it as Outlook is shutting down, or cobble something together with reminders to trigger the macro.

  5. Dave mack says

    What I really need is a macro to turn off the Out-Of-Office setting when I get back in the office. Like Office 2003 used to do and Office 2010 does not.

  6. Tom Edwards says

    Hi Diane,

    I would love to be able to disable/enable outlook rules based on the content of a received email. I'm running Outlook 2013, using an exchange account. I have a bunch of rules that redirect emails based on a time schedule. I would also like to be able to interrupt that schedule if required.

    I need this to run on the server-side as Outlook won't actually be running 24/7, is there any way to achieve this?

    Thanks

    • Diane Poremsky says

      Outlook doesn't have anything built in that can do this. If you have exchange, Out of office has some rules capability and that may be enough to meet your needs. Its not automatic but in newer versions you can set it ahead of time - it will turn itself on and off. But you will need to keep setting the start time.

      VBA is out because you can't run it on the server.

  7. Tom Edwards says

    Thanks Diane for your reply, perhaps I will just run Outlook on the PC 24/7 which probably wont be a problem.

    So, if I want to look for a string within the body of an email and enable/disable a rule based on the string being present - will the above work for me? I cant seem to get it to appear on the scripts under the rules wizard.

    Thanks

    • Diane Poremsky says

      if you want to turn on a rule based on a message that was received, plug the enable/disable code into a run a script rule.

      if possible, let the rule look for the keyword then call this script -
      Sub DisableRule(Item As Outlook.MailItem)
      Disable_Run_Rule
      End Sub

      You can either call the rule macro or put the rule code in the script.

  8. Alejandro says

    Hello, I have Outlook 2007 and do not appear in the list macros that might be missing.
    Thank you! (from Argentina)

    • Diane Poremsky says

      Macros with something in the () - like this: Private Sub Application_Reminder(ByVal Item As Object) are not listed in the macros you can run manually. They run automatically when something happens.

  9. Raman Deep Singh says

    Hi, I have outlook 2007. I need a script to run two minutes after I start outlook so that the mailbox has received all new mails. The script
    scans for a particular mail and if it hasnt arrived triggers a reminder
    or a task(someway to alert the user) and keeps reminding him every hour
    until the mail arrives. When the mail arrives, this task or reminder should be disabled or deleted.
    Can you give me some leads for this.
    Also can you tell me is it possible to run a script when a particular task fires.just as we can "run a script" if a particular mail arrives by using rules and alerts.

    • Diane Poremsky says

      Hmm. I thought I answered this last week. :( You can run macros when a reminder fires. Scanning for an email is harder but doable as sperry software and others have addins that can look for sent mail that needs followed up. I don't have any code sample, but it would be fairly complicated - sperry's addin may be a more cost effective option over writing something.

  10. Christopher says

    Hi Diane!
    I can't seem to get the script to work. I've changed some names in order to keep track of what rules it applies. Please have a look

    Private Sub Schema_SOA(ByVal Item As Object)

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

    If Item.Subject = "Enable Rule SOA" Then
    Enable_Run_Rule_SOA
    End If

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

    End Sub

    • Diane Poremsky says

      If you run the Sub Enable_Run_Rule_SOA() or Sub Disable_Run_Rule_SOA() by clicking the Run button, do they work? (I'm assuming you renamed Enable_Run_Rule and Disable_Run_Rule and entered the rule name in the enable and disable macros. )

  11. Christopher says

    I trigger the rules by incoming e-mails and yes they work on their own.
    You are correct I've changed the names. but when I compile It shows error on the "Enable_Run_Rule_SOA"

    • Diane Poremsky says

      Does it give you any error codes? AS long as you renamed the disable macro to match, it should work.

    • Diane Poremsky says

      That often means a typo - word's curly "smart" quotes instead of straight quotes, 2 single quotes instead of a double... something small and easy to overlook. The code you posted looks fine. If the enable macro looks like it does on the webpage, it should work

  12. Christopher says

    It doesn't work, I've attached all code below.

    "
    Sub Disable_Run_Rule_SOA(Item As Outlook.MailItem)
    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("SOA")
    olRule.Enabled = False
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Enable_Run_Rule_SOA(Item As Outlook.MailItem)
    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("SOA")
    olRule.Enabled = True
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub

    Sub Enable_Run_Rule_WebAccessNewPin(Item As Outlook.MailItem)
    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("WebAccessNewPin")
    olRule.Enabled = True
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Disable_Run_Rule_WebAccessChangePin(Item As Outlook.MailItem)
    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("WebAccessChangePin")
    olRule.Enabled = False
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Enable_Run_Rule_WebAccessChangePin(Item As Outlook.MailItem)
    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("WebAccessChangePin")
    olRule.Enabled = True
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Disable_Run_Rule_WebAccessCheckChannels(Item As Outlook.MailItem)
    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("WebAccessCheckChannels")
    olRule.Enabled = False
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Enable_Run_Rule_WebAccessCheckChannels(Item As Outlook.MailItem)
    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("WebAccessCheckChannels")
    olRule.Enabled = True
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Disable_Run_Rule_WebAccessNewPin(Item As Outlook.MailItem)
    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("WebAccessNewPin")
    olRule.Enabled = False
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Disable_Run_Rule_WebAccessPermanentLogin(Item As Outlook.MailItem)
    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("WebAccessPermanentLogin")
    olRule.Enabled = False
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub
    Sub Enable_Run_Rule_WebAccessPermanentLogin(Item As Outlook.MailItem)
    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("WebAccessPermanentLogin")
    olRule.Enabled = True
    If blnExecute Then olRule.Execute ShowProgress:=True
    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing
    End Sub

    Sub Schema_SOA(ByVal Item As Object)

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

    If Item.Subject = "Enable Rule SOA" Then
    Enable_Run_Rule_SOA
    End If

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

    End Sub

    Sub Schema_SIGNICAT(ByVal Item As Object)

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

    If Item.Subject = "Enable Rule SIGNICAT" Then
    Enable_Run_Rule_WebAccessNewPin
    Enable_Run_Rule_WebAccessChangePin
    Enable_Run_Rule_WebAccessCheckChannels
    Enable_Run_Rule_WebAccessPermanentLogin
    End If

    If Item.Subject = "Disable Rule SIGNICAT" Then
    Disable_Run_Rule_WebAccessNewPin
    Disable_Run_Rule_WebAccessChangePin
    Disable_Run_Rule_WebAccessCheckChannels
    Disable_Run_Rule_WebAccessPermanentLogin
    End If

    End Sub
    "

    In Desperate times...

    • Diane Poremsky says

      Do the rules work individually?
      comment out the subject and replace with
      Sub Test_rule()
      Dim Item As Outlook.MailItem
      Test the enable and disable rules.

      How are you calling the two schema_ macros?

  13. Christopher says

    The rules work individually but not the appointment one. regards the "schema_" I try to use it when I receive a notice that I've got an appointment

  14. Jacob Mabb says

    On exchange 2007, I used 2 powershell scheduled tasks (One to enable, one to disable). The powershell scheduled tasks must be run as the mailbox user, however, this does NOT require the user to be logged into the machine or outlook to be running. It launches outlook for that user at the time of the scheduled task and enables/disables the rule.

    #Enable Rule Script
    $ol = New-Object -ComObject Outlook.Application
    $ol.visible = $true
    $namespace = $ol.GetNamespace("MAPI")
    $inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
    $rules = $namespace.DefaultStore.GetRules()
    $rule = $rules | Where-Object { $_.Name -eq "" }
    $rule.Enabled = $True
    $rules.Save()

    #Disable Rule Script
    $ol = New-Object -ComObject Outlook.Application
    $ol.visible = $true
    $namespace = $ol.GetNamespace("MAPI")
    $inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
    $rules = $namespace.DefaultStore.GetRules()
    $rule = $rules | Where-Object { $_.Name -eq "" }
    $rule.Enabled = $False
    $rules.Save()

    • Jacob Mabb says

      $rule = $rules | Where-Object { $_.Name -eq "" }

      You must put the name of the rule between these double-quotes.

  15. Dean says

    Hi,

    I'm looking to implement this as a button on a toolbar. While i can create a toolbar, can you help get a link between the macro and a button?

    thanks

    • Diane Poremsky says

      What version of outlook? In 2010 and 2013 you select the macro in the customize ribbon dialog. In 2007, you add the macro to the QAT. In older versions you customize the toolbar and add the macro to it.

Leave a Reply

Please post long or more complicated questions at OutlookForums by Slipstick.com.

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