• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1189
  • 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 BottomleySoftware Quality Lead EngineerCommented:
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 BottomleySoftware Quality Lead EngineerCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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 BottomleySoftware Quality Lead EngineerCommented:
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 BottomleySoftware Quality Lead EngineerCommented:
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 BottomleySoftware Quality Lead EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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