Rules.Create メソッド (Outlook)

Name で指定された名前と RuleType で指定された規則の種類を持つ Rule オブジェクトを作成します。

構文

expression. Create( _Name_ , _RuleType_ )

Rules オブジェクトを表す変数。

パラメーター

名前 必須 / オプション データ型 説明
名前 必須 String ルールの作成後に Rule.Name で表される、ルールの文字列識別子を指定します。 コレクション内のルール名は一意ではありません。
RuleType 必須 OlRuleType 送信または受信メッセージにルールが適用されるかを示す OlRuleType 列挙の定数です。

戻り値

新しく作成されたルールを表す Rule オブジェクト。

注釈

追加された ルールの RuleType パラメーターは、 Rule オブジェクトに関連付けることができる有効なルール アクション、ルール条件、およびルール例外条件を決定します。

ルールがコレクションに追加されると、新しいルールの Rule.ExecutionOrder は 1 になります。 コレクション内の他のルールの ExecutionOrder は、1 ずつ増加します。

Visual Basic for Applications (VBA) の次のコード サンプルでは、Rules オブジェクト モデルを使用してルールを作成します。 ここでは、 RuleAction オブジェクトと RuleCondition オブジェクトを使用して、メッセージの件名に特定の語句が含まれている場合を除き、特定の送信者から特定のフォルダーにメッセージを転送するルールを指定します。 ここでは、受信トレイに "Dan" というフォルダーが既に存在することを前提としています。

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

関連項目

ルール オブジェクト

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。