More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-May-2019 09:51

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: OL2007: Run all rules against inbox
Description: Outlook 2007 provides full programmatic access to rules, including the ability to execute them programmatically. This Outlook VBA sample also introduces the new Store object and DefaultStore property.
Date: 31-May-2006  11:16
Code level: beginner
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 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
    ' 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

All 110comments
Page [ 1 2 3 4 5 6 7 8 9 10 Next >>  
  31-May-2006  12:14   
The Rule.Execute statement supports parameters to show a progress box, to run the rule against a specific folder, with or without subfolders, and to operate against read, unread, or all messages in the folder. The default behavior is to run it against all messages in the Inbox, but not its subfolders, with no progress box.

For more information, see
  04-Jun-2006  00:37   
Can we expect a new 'Outlook Programming' book from Sue with the release of Outlook 2007? If yes, when?!
  04-Jun-2006  08:24   
Peter, thanks for asking. Yes, I'm working on an Outlook 2007 Programming book, hopefully to be released about the same time as the product.
  04-Jun-2006  09:20   
Excellent! I am sure I am not alone in my desire to purchase the next version of the Outlok 'bible'!
  05-Jul-2006  13:03   

Is there a way to make this work with Outlook 2003?

  05-Jul-2006  13:10   
Ray, no, because Outlook 2007 is the first version to support programmatic creation and execution of rules.
  03-Jan-2007  15:36   
Sue - while you state that it is not possible create from scratch or run rules programmatically in Outlook 2003. Is it possible in 2003 import a rule programmatically based on a rule export file template?
  03-Jan-2007  17:11   
No, Rich, Outook 2007 is the first version of Outlook to have any programmatic support for rules at all.
  13-Feb-2007  05:06   
What to do if I want to run rules on another folder?
  13-Feb-2007  11:02   
Hasan, look in the object browser at the signature for the Rule.Execute method and you'll see that it supports an optional folder parameter.
Page [ 1 2 3 4 5 6 7 8 9 10 Next >>