none
How to run Outlook 2007/2010 Rules from a button RRS feed

  • Question

  • having some issues running this vb script.  I want to have a macro button to run on the fly my rules in Outlook 2007 instead of always clicking and checking them manually. I found this script but to no avail doesnt work even after i cleaned it up.  im stuck on this part of it:    Sub RunAllInboxRules()    it says compile error sub or function not defined.

    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

    Thursday, April 10, 2014 10:58 PM

Answers

  • Hello,

    Where did you paste the code listed above? VBScript and VBA are different. See Sub or Function not defined (Visual Basic) for more information about the error.

    Anyway, I have just tried to paste the code to my VBA module and run it. I've got the Compile error too. Then I replaced the ' and " symbols in the code and the code started working:

    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
    
      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

    Check out comments and string declarations in the code.

    • Marked as answer by Marvin_Guo Friday, April 18, 2014 1:43 AM
    Friday, April 11, 2014 10:56 AM

All replies

  • Hello,

    Where did you paste the code listed above? VBScript and VBA are different. See Sub or Function not defined (Visual Basic) for more information about the error.

    Anyway, I have just tried to paste the code to my VBA module and run it. I've got the Compile error too. Then I replaced the ' and " symbols in the code and the code started working:

    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
    
      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

    Check out comments and string declarations in the code.

    • Marked as answer by Marvin_Guo Friday, April 18, 2014 1:43 AM
    Friday, April 11, 2014 10:56 AM
  • so you removed the ' and " or the ` and the "  then it worked?   does this mean that none of those these are needed in the code?
    Friday, April 11, 2014 8:55 PM
  • Not so - I just replaced them. It looks like they were corrupted when you copied the source code from a web page. I think it is related to encoding. So, you need copy the correct ones from my previous post there.

    Friday, April 11, 2014 9:08 PM