Share via


Outlook Rules!

Rules are evaluated in sequence according to the increasing order of this value. The evaluation order for rules that have the same value in this property is undefined.
PidTagRuleSequence: https://msdn.microsoft.com/en-us/library/dd188686.aspx

The Rule.ExecutionOrder property could be used to assign priority to rules. Rules.Item(1) represents a rule with ExecutionOrder being 1, Rules.Item(2) represents a rule with ExecutionOrder being 2, and Rules.Item(Rules.Count) represents the rule with ExecutionOrder being Rules.Count.
https://msdn.microsoft.com/en-us/library/bb176165.aspx

The below VBA script changes the Execution order of the 2nd rule whose execution order is 2nd to last.
Whenever the order for the rule x changed to x+n, all the rules from x+1 till x+n is shifted up.

E.g. If there are 5 rules and the 2nd rule whose Execution order is 2nd is changed to 5:
Rule 3 execution order is changed to 2
Rule 4 execution order is changed to 3
Rule 5 execution order is changed to 4
Rule 2 execution order is changed to 5

 Sub CreateRule()
    Dim colRules As Outlook.Rules
    Dim oRule As Outlook.Rule
    Dim colRuleActions As Outlook.RuleActions
    Dim oMoveRuleAction As Outlook.MoveOrCopyRuleAction
    Dim oFromCondition As Outlook.ToOrFromRuleCondition
    Dim oExceptSubject As Outlook.TextRuleCondition
    Dim oInbox As Outlook.Folder
    Dim oMoveTarget As Outlook.Folder
    Dim intCounter As Integer
    Dim oRuleCount As Integer 

    Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
    'Get Rules from Session.DefaultStore object
    Set colRules = Application.Session.DefaultStore.GetRules()
    oRuleCount = colRules.Count
    Debug.Print oRuleCount
    intCounter = 1
    Do While intCounter < (colRules.Count + 1)
        Set oRule = colRules.Item(intCounter)
        Debug.Print oRule.Name
        Debug.Print oRule.ExecutionOrder
        intCounter = intCounter + 1
    Loop
    Set oRule = colRules.Item(2)
    oRule.ExecutionOrder = oRuleCount
    'Update the server and display progress dialog
    colRules.Save
End Sub

If a rule is created using the Outlook Rules Wizard, its value is least and has highest priority. The existing rules gets pushed down by 1 i.e. PR_RULE_SEQUENCE gets increased by 1 for all the existing rules.
Conversely, a new rule gets higher priority when created using Outlook wizard or using Outlook Object model programmatically.

Comments