• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1181
  • Last Modified:

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

0
jfexchange
Asked:
jfexchange
  • 5
  • 3
2 Solutions
 
Chris BottomleyCommented:
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
0
 
jfexchangeAuthor Commented:
no i am using 2007; sp1.  
0
 
Chris BottomleyCommented:
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jfexchangeAuthor 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,
0
 
Chris BottomleyCommented:
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
0
 
Chris BottomleyCommented:
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
0
 
Chris BottomleyCommented:
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

0
 
jfexchangeAuthor 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!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now