Create a Rule to Move Specific Emails to a Folder
This topic shows a code sample in Visual Basic for Applications (VBA) that uses the Rules object model to create a rule. The code sample uses the RuleAction and RuleCondition objects to specify a rule that moves messages from a specific sender to a specific folder, unless the message contains certain terms in the subject. Note that the code sample assumes that there already exists a folder named "Dan" under the Inbox.
The following describes the steps used to create the rule:
Specify the target folder
oMoveTargetto move specific messages as determined by the condition and exception condition. The target folder is a subfolder named "Dan" under the Inbox, and is assumed to already exist.
Use Store.GetRules to obtain a set of all the rules in the current session.
Using the Rule object returned from the last step, use the RuleConditions.From property to obtain a ToOrFromRuleCondition object,
oFromConditionspecifies the condition for the rule: when a message is from
Using the same Rule object, use the RuleActions.MoveToFolder property to obtain a MoveOrCopyRuleAction object,
oMoveRuleActionspecifies the action for the rule: move the message to the target folder "Dan".
Using the same Rule object, use the RuleConditions.Subject property to obtain a TextRuleCondition object,
oExceptSubjectspecifies the exception condition: if the subject contains the terms "fun" or "chat", then do not apply the rule to move the message to the folder "Dan".
Use Rules.Save to save the new rule together with the rest of the rules for the current store.
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("Dan's rule", olRuleReceive) 'Specify the condition in a ToOrFromRuleCondition object 'Condition is if the message is from "Dan Wilson" Set oFromCondition = oRule.Conditions.From With oFromCondition .Enabled = True .Recipients.Add ("Dan Wilson") .Recipients.ResolveAll 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 = _ oRule.Exceptions.Subject With oExceptSubject .Enabled = True .Text = Array("fun", "chat") End With 'Update the server and display progress dialog colRules.Save End Sub
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.