Outlook Rules RRS feed

All replies

  • Hi Seven,

    You can use the following code to create an outlook rule on all accounts:

    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("Dan") 
       '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("Eugene's rule", olRuleReceive) 
       'Specify the condition in a ToOrFromRuleCondition object 
       'Condition is if the message is sent by "DanWilson" 
       Set oFromCondition = oRule.Conditions.From 
       With oFromCondition 
        .Enabled = True 
        .Recipients.Add ("Eugene Astafiev") 
       End With 
       '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 oExceptSubject = _ 
       With oExceptSubject 
        .Enabled = True 
        .Text = Array("fun", "chat") 
       End With 
       'Update the server and display progress dialog 
     End Sub 

    For more information, please review the following link:

    Create/Copy (this rule on all accounts) outlook using vba

    Hopefully it helps you.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, October 30, 2018 2:07 AM
  • Thanks, Yuki. Do I have to run this code everytime a new user/mailbox is created?
    Tuesday, October 30, 2018 3:09 PM