How to: Create a Rule to Move Specific E-mails to a Folder
|Outlook Developer Reference|
This topic shows a code sample in Visual Basic for Applicatons (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 Rules collection returned from the last step, use Rules.Create to add a new rule. The new rule specifies some action upon receiving a message, so it is of type olRuleReceive.
- Using the Rule object returned from the last step, use the Rule.Conditions.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 Rule.Actions.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 Rule.Exception.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.