Google

2007-10-04

Enable / Disable Outlook 2007 Rules


As a Blackberry user, one of my pain-points is with the pst files. Here is how it goes. Let's say Outlook is open and I get an e-mail, my rules process it and move it to one of the pst folders. Problem is that I don't get it in my BlackBerry.

Of course, if outlook was closed, or if the rules were not processed, that message would end up in my BlackBerry. As I want to keep outlook open, I have to disable the rules. There is no manual way of quickly doing this. If you look at Outlook Rules > Run Rules, you can not, for example, select all with Ctrl+a. You have to click and select each one of them. Painful!

So, I decided to create a macro, that would automatically disable the rules, or enable & run them. Idea is that in the morning, I will keep the rules disabled so that I will get them in my BB, and time to time I will run enable script to move them to psts.

Below is the code. In Outlook, you can
* press Alt+F11
* Expand Project1 (VbaProject.OTM)
* Expand Microsoft Office Outlook Objects
* double click ThisOutlookSession
and paste the code in there.

Then you can simply Press Alt+F8 and chose the action...


Sub DisableAllRules()
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule
Dim count As Integer
Dim ruleList As String
'On Error Resume Next

'Get Rules from Session.DefaultStore object
Set colRules = Application.Session.DefaultStore.GetRules

' iterate all the rules
For Each oRule In colRules
oRule.Enabled = False
count = count + 1
ruleList = ruleList & vbCrLf & count & ". " & oRule.Name
Next

colRules.Save

' tell the user what you did
ruleList = "These rules were enabled: " & vbCrLf & ruleList
MsgBox ruleList, vbInformation, "Macro: DisableAllRules"

Set colRules = Nothing
Set oRule = Nothing
End Sub

Sub Enable_Run_AllRules()
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule
Dim count As Integer
Dim ruleList As String
'On Error Resume Next

'Get Rules from Session.DefaultStore object
Set colRules = Application.Session.DefaultStore.GetRules

' iterate all the rules
For Each oRule In colRules
oRule.Enabled = True
oRule.Execute ShowProgress:=True
count = count + 1
ruleList = ruleList & vbCrLf & count & ". " & oRule.Name
Next

colRules.Save

' tell the user what you did
ruleList = "These rules were enabled: " & vbCrLf & ruleList
MsgBox ruleList, vbInformation, "Macro: EnableAllRules"

Set colRules = Nothing
Set oRule = Nothing
End Sub

14 comments:

2sms said...

Works brilliantly! Thank-you very much. Tim.

Adil Hindistan said...

How nice to know it helped someone :)

Anonymous said...

Hello will it work in outlook 2003 ?

Adil Hindistan said...

I have not tested it in O2K3 but I don't think it would be a problem.

Anonymous said...

This works like a charm, I have modified it slightly to be a better fit for me though. Instead of it being 2 options, I just but the disable portion at the bottom of the enable and run. This was it enables all rules, runs them, and disables them.

Anonymous said...

Any way that you can show / tell me how this might work in Outlook 2003? I've just tried editing the code but am not very good at it and can't get it to work! Much appreciated!

maxx00 said...

Unfortunately this does not show the Oof rules. Can you tell me how to get them? Thanks!

J Bishop said...

Macros are so much easier than writing an Add-in. Thanks for your post!

I tweaked this script to only engage / disengage my forwarding rule. I can turn it on remotely or off remotely.

Worked great!

Anonymous said...

Thanks !!! Very useful.

Anonymous said...

Dear Adil,
These two macros of yours are very useful for people having too many folders that they cannot follow as outlook shuffles all emails. For long I have wanted to read messages first and then run rules manually for all messages in the inbox. Your macro may be slightly modified to run the macros one by one for the inbox before enabling them all.
kind regards,
ahmet turer.

Anonymous said...

This script will NOT work in Outlook 2003, because there is no "rules"-object.

Anonymous said...

Sir,

You are a gentleman and a scholar. And you just saved me a HUGE amount of time.

Thank you.

Anonymous said...

Hello, How do I do to enable/disable one of my rules only?

Thanks

Al

Anonymous said...

awesome!!! Thank you very much, I will have much use for this.