How to run Outlook 2007/2010 Rules from a button

I was asked how a user could manually run all or a single rule, without having to use the outlook rules dialog box. here is how you do it.

First, go into the VB Editor,  Tools -> Macro’s > Visual Basic Editor (or press ALT-F11)

assuming you dont already have any modules in here.  Press Insert -> Module

you will be presented with a new window waiting for code, paste this in for all rules:


Sub RunAllInboxRules()
Dim st As Outlook.Store
Dim myRules As Outlook.Rules
Dim rl As Outlook.Rule
Dim count As Integer
Dim ruleList As String
‘On Error Resume Next

‘ get default store (where rules live)
Set st = Application.Session.DefaultStore
‘ get rules
Set myRules = st.GetRules

‘ iterate all the rules
For Each rl In myRules
‘ determine if it’s an Inbox rule
If rl.RuleType = olRuleReceive Then
‘ if so, run it
rl.Execute ShowProgress:=True
count = count + 1
ruleList = ruleList & vbCrLf & rl.Name
End If
Next

‘ tell the user what you did
ruleList = “These rules were executed against the Inbox: ” & vbCrLf & ruleList
MsgBox ruleList, vbInformation, “Macro: RunAllInboxRules”

Set rl = Nothing
Set st = Nothing
Set myRules = Nothing
End Sub

______________________________________________________________________________________________________________________________
and this for a single rule, dont forget to change rulename

____________________________________________________________________________________________________________________________________

Sub RunAllInboxRules()
Dim st As Outlook.Store
Dim myRules As Outlook.Rules
Dim rl As Outlook.Rule
Dim runrule As String
dim rulename as string

Rulename = “name of rule”

Set st = Application.Session.DefaultStore

Set myRules = st.GetRules

For Each rl In myRules

If rl.RuleType = olRuleReceive Then

If rl.Name = rulename Then
rl.Execute ShowProgress:=True
runrule = rl.Name

End If
End If
Next

ruleList = “This rule was executed against the Inbox:” & vbCrLf & runrule
MsgBox ruleList, vbInformation, “Macro: RunAllInboxRules”

Set rl = Nothing
Set st = Nothing
Set myRules = Nothing
End Sub


VB Editor in MS Outlook 2007

This will create a new macro that runs all the rules one by one in against your inbox.

Now create a button, goto View -> Tool bars -> Customize
go into the Toolbas Tab, and click New, call it what you want. I called mine “Rules”

The toolbar will be created floating, you can drag this now where you want it in the client. or leave it alone for now.
then go into the Commands Tab, scroll down to Macro’s on the left hand side, you should see Project1.Runallinboxrules in there. drag that up to the new toolbar and release it. This will make a new icon to the macro.

you can right click the icon to customise it. To bring it inline with the rest of the toolbars, I set mine to ‘Default Style’

Then you can close the Toolbar config window. and test it out. it will tell you which rules were run when it finishes

macro setup in outlook 2007

  1. No comments yet.
(will not be published)