Link to home
Start Free TrialLog in
Avatar of jfexchange
jfexchange

asked on

Run all MSOutlook rules at once

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

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of jfexchange
jfexchange

ASKER

no i am using 2007; sp1.  
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

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,
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
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!