Rules.Create Method (Outlook)

Creates a Rule object with the name specified by Name and the type of rule specified by RuleType.

Version Added: Outlook 2007

expression .Create(Name, RuleType)

expression A variable that represents a Rules object.

Parameters

Name

Required/Optional

Data Type

Description

Name

Required

String

A string identifier for the rule, which will be represented by Rule.Name after rule creation. Names of rules in a collection are not unique.

RuleType

Required

OlRuleType

A constant in the OlRuleType enumeration that determines whether the rule is applied on sending or receiving a message.

Return Value

A Rule object that represents the newly created rule.

The RuleType parameter of the added rule determines valid rule actions, rule conditions, and rule exception conditions that can be associated with the Rule object.

When a rule is added to the collection, the Rule.ExecutionOrder of the new rule is 1. The ExecutionOrder of other rules in the collection is incremented by 1.

The following code sample in Visual Basic for Applicatons (VBA) uses the Rules object model to create a rule. The code sample uses the RuleAction and RuleCondition objects to specify a rule that forwards 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 "Dan" under the Inbox.

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 sent by "DanWilson" 
 
 Set oFromCondition = oRule.Conditions.From 
 
 With oFromCondition 
 
 .Enabled = True 
 
 .Recipients.Add ("DanWilson") 
 
 .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 
 

Concepts

Other resources

Show:
© 2015 Microsoft