Class Rules (Outlook VBA)
The class Rules represents a set of Rule objects that are the rules available in the current session. To use a Rules class variable it first needs to be instantiated, for example
Dim rls as Rules
Set rls = Session.DefaultStore.GetRules()
Class
Returns an OlObjectClass constant indicating the object's class. Here you can find possible values for
Dim oocsClass As OlObjectClass
oocsClass = Session.DefaultStore.GetRules.Class
Count
Returns a Long indicating the count of objects in the specified collection.
Dim lngCount As Long
lngCount = Session.DefaultStore.GetRules.Count
Create
Creates a Rule object with the name specified by Name and the type of rule specified by RuleType.
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.
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
Arguments
The following arguments are required:
Name (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 (OlRuleType) - A constant in the OlRuleType enumeration that determines whether the rule is applied on sending or receiving a message.
Possible return values are olRuleReceive - Indicates that the rule is applied to messages that are being received, olRuleSend - Indicates that the rule is being applied to messages being sent.
IsRssRulesProcessingEnabled
Returns or sets a Boolean that indicates whether RSS rules processing has been enabled.
After setting IsRssRulesProcessingEnabled, you must call Rules.Save to persist this setting. This property is persisted on a mailbox-level setting that will roam with the user. If IsRssRulesProcessingEnabled is False, then no conditions about RSS feeds will be evaluated during rules processing.
Session.DefaultStore.GetRules.IsRssRulesProcessingEnabled = True
Item
Obtains a Rule object specified by Index , which is either a numerical index into the Rules collection or the rule name.
Returns an error when the rule cannot be found in the collection.
Item (Index)
Index: Either a 1-based long value representing an index into the Rules collection, or a string name representing the value of the default property of a rule, Rule.Name.
Dim rul As Rule
Set rul = Session.DefaultStore.GetRules(Index:=1)
Remove
Removes from the Rules collection a Rule object specified by Index , which is either a numerical index into the Rules collection or the rule name.
Returns an error when the rule cannot be found in the collection.
Remove (Index)
Index: Either a long value representing an index into the Rules collection, or a string name representing the value of the default property of a rule, Rule.Name.
Dim lngIndex As Long: lngIndex =
Session.DefaultStore.GetRules.Remove Index:=lngIndex
Save
Saves all rules in the Rules collection.
After you enable a rule, you must also save the rule by using Rules.Save so that the rule and its enabled state will persist beyond the current session. A rule is only enabled after it has been saved successfully. Rules.Save can be an expensive operation in terms of performance on slow connections to Exchange server. For more information on using the progress dialog box, see Manage Rules in the Outlook Object Model. Saving rules that are incompatible or have improperly defined actions or conditions (such as an empty string for TextRuleCondition.Text) will return an error. The Exchange server limits the maximum number of rules that can be supported by a store. Rules.Save returns an error when this limit is reached.
Save (ShowProgress)
ShowProgress: True to display the progress dialog box, False to save rules without showing the progress.
Session.DefaultStore.GetRules.Save
Session
Returns the NameSpace object for the current session.
The Session property and the GetNamespace method can be used interchangeably to obtain the NameSpace object for the current session. Both members serve the same purpose. For example, the following statements perform the same function: