We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Run all MSOutlook rules at once

Medium Priority
1,241 Views
Last Modified: 2013-11-05
Hello, i have a number of MS Outlook rules which I am trying to run at once.  I found a VBS script for this online here:  http://www.outlookcode.com/codedetail.aspx?id=1266

However, i get a compilation error when I try to run this script.  Can someone advise what the issue might be?  Thanks in advance~!
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

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2011

Commented:
You don't say which version of outlook you are using but I suspect 2003 or earlier since the stores collection was introduced in 2007 so I suspect that is the root of the problem.

Chris

Author

Commented:
no i am using 2007; sp1.  
CERTIFIED EXPERT
Top Expert 2011

Commented:
ANy of the rules not working properly then:

Chris


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
        on error goto oops
        ' 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
oops:
    Next
    on error goto 0
    
    ' 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

Open in new window

Author

Commented:
thanks i ran that, and still go the same error:

PS C:\Users\vista\Desktop> cscript rulesowa.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Users\vista\Desktop\rulesowa.vbs(2, 12) Microsoft VBScript compilation error: Expected end of statement


any idea what the issue is?  Thanks,
CERTIFIED EXPERT
Top Expert 2011

Commented:
I really ought to read more.  You mention VBS in your intro but the macro as posted is VBA not VBS.  i.e. it should reside in a code module not in a script.  Do you need it restructuring for a script, (should be ok) or are you ok to run it from outlook?

Chris
CERTIFIED EXPERT
Top Expert 2011
Commented:
Alt + F11 to open the macro editor
     Insert | Module to insert a code module into the project
     In the project tree select the module.
     Insert the required macro(s) into the selected module, (RunAllInboxRules)
Close the Visual Basic Editor.

Check Security as appropriate:
------------------------------

In the application select Tools | Macro | Security
Select Medium
Select OK

To run a macro:
---------------

Alt + F8
Select the macro
Select 'Run'

Chris

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2011
Commented:
If you do want a VBS script then try:

Chris
Dim olkApp
Dim st
Dim myRules
Dim rl
Dim count
Dim ruleList
const olRuleReceive = 0
    
    ' get default store (where rules live)
    set olkApp = createobject("outlook.application")
    Set st = olkapp.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 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

Open in new window

Author

Commented:
Chris thanks for looking this over.   I also had misread the original URL and  thought the snippet was vbs.  I was able to run the macro as VBA.   Thanks for the conversion  over to VBS as well!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.