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.
In 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
A total newbie. I have done what you say at the start but I am confused as to how the marco will know what rule to run if I apply it to multiple reminders. Also once I have created the macro what do I do then?
Thank you very much
Hi, sorry for asking a newbie question. I have entererd your macro 1:1 in my outlook 2019. The macro doesnt start either. If i start it in the VBA editor there is a error missing object. How can i solve this problem?
Thanks and regards Marius
hi! I'm a beginner :) but was looking to try and create a macro that would turn on or off a specific rule based on my input (I would then assign that Macro to a toolbar in the ribbon) Anyone created that previously per chance?
Do you want to call the macro from a button? The macros named Sub Enable_Run_Rule() and Sub Disable_Run_Rule() that are part of the reminder macro can be run manually.
Hello! I know this is an old post, but I hope someone can help me out anyway.
I have created the following:
- a rule called "TEST-OOO"
- the Enable and Disable appontments with reminders
- the VBA, changing "Your Rule Name" to "TEST-OOO" and replacing IPM.Task with IPM.Appointment
The Enable reminder shows at the time set, but the rule is not enabled.
What have I misunderstood? Do I need to do something else?
To the best of my recollection, that should work. What version and build of Outlook? I know Outlook 2016 tightened some security around rules but i didn't think it affected this.
Add some MsgBox at various points to see if they come up, starting with the enable and disable functions. Add a message box in the Enable_Run_Rule and disable subs too - somewhere near the top.
If Item.Subject = "Enable Rule" Then
MsgBox "Enable Rule fired"
Enable_Run_Rule
end if
I just tested it here in Outlook 2016, it is working.
Dear Diane,
I tried to write you an answer, but I believe the message got "hung" somewhere...
Thank you for your prompt reply, it was very helpful. I was using Outlook 2010 and couldn't get it to work. But when I switched to a computer with Outlook 2016 it worked like a charm :)
But I encountered another "challenge" that you can possibly assist with.
For the auto reply rule I used the "reply using a specific template" option. This apparently only sends out the auto reply once for each sender. I would very much like it to send the auto reply every time for every sender, regardless of how many emails i receive from the sender.
Is is possible to achieve this somehow?
Thank you for your assistance, it is highly appreciated :)
Regards, Vidar
It's 'hung' in the moderation queue - i leave everything there until i have time to reply, as it makes it easier to find the messages that need answered.
Reply with template is one per sender until outlook is restarted. if you use a run a script macro to send the template, it will reply to all messages from the sender.
https://www.slipstick.com/developer/run-a-script-rule-autoreply-using-a-template/
Oh, did you put it in thisoutlooksession? Because its triggered automatically, it needs to be in thisoutlooksession, not a module. Also, i'm assuming you set macro security to low for testing?
Dear Diane,
First of all, thank you for this nice solution. It has really helped me a lot!
Most of the time the Outlook rule gets enabled/disabled as it should. However, it happens from time to time that the rule is NOT enabled/disabled.
I don't know the reason for this, but I would like to not having to check this manually twice a day.
Is there a way to have Outlook send me an email every time the rule is actually enabled/disabled, just to let me know everything is as it should be?
Thank you for all your assistance, it is much appreciated :)
Regards, Vidar
Sure. Use the macro at https://www.slipstick.com/developer/create-a-new-message-using-vba/ - removing the fields you don't need. You can use the macro with both enable/disable. Change .display to .send. You can call the macro after rule is changed. If Item.Subject = "Disable Rule" Then Disable_Run_Rule End If CreateNewMessage End Sub Or put this at the end, instead of the macro name: Dim objMsg As MailItem Set objMsg = Application.CreateItem(olMailItem) With objMsg .To = "Alias@domain.com" .Subject = item.subject .Body= item.subject .Send End With Set objMsg = Nothing This will only tell you if the macro ran - it doesn't confirm the change was made. It will be possible to get the status after the it runs, so you know if the macro worked. Something like this, but i didn't test it so it might not be quite right. Basically, we want to get the status if the rule, on or off and put the value in the body. It will either be a 1 or 0. Use it with the first example. Public Sub CreateNewMessage() Dim objMsg As 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("Your Rule Name")… Read more »
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
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.
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()
$rule = $rules | Where-Object { $_.Name -eq "" }
You must put the name of the rule between these double-quotes.
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