Outlook : Delete a rule using VBA

Joined
Aug 17, 2011
Messages
1
Reaction score
0
Hi all,

I created a macro to add a filtering rule in Outlook 2007.
Each day, this rule must be updated. Do you know how I can specifiy in the beginning of my macro to look for the existing rule, if found, delete it and then create the updated rule. Like a daily update of the rule if you prefer...

Thanks

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

    'Specify target folder for rule move action
    Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
    'Assume that target folder already exists
    Set oMoveTarget = oInbox.Folders("Test")
    
    'Get Rules from Session.DefaultStore object
    Set colRules = Application.Session.DefaultStore.GetRules()
    
    'Create the rule by adding a Receive Rule to Rules collection
    Set oRule = colRules.Create("Test's rule", olRuleReceive)

    'Specify the action in a MoveOrCopyRuleAction object
    'Action is to move the message to the target folder
    Set oMoveRuleAction = oRule.Actions.MoveToFolder
    With oMoveRuleAction
        .Enabled = True
        .Folder = oMoveTarget
    End With

    'Specify the exception condition for the subject in a TextRuleCondition object
    'Exception condition is if the subject contains "fun" or "chat"
    Set oConditionSubject = oRule.Conditions.Subject
    With oConditionSubject
        .Enabled = True
        .Text = Array("bla", "gla")
    End With

    'Update the server and display progress dialog
    colRules.Save
End Sub
 
Back
Top