Erstellen einer Regel zum Verschieben bestimmter E-Mails in einen OrdnerCreate a Rule to Move Specific Emails to a Folder

In diesem Thema wird ein Codebeispiel in Visual Basic for Applications (VBA) gezeigt, in dem das Rules -Objektmodell zum Erstellen einer Regel verwendet wird.This topic shows a code sample in Visual Basic for Applications (VBA) that uses the Rules object model to create a rule. Das Codebeispiel verwendet die RuleAction- und RuleCondition-Objekte zum Angeben einer Regel, die Nachrichten eines bestimmten Absenders in einen bestimmten Ordner verschiebt, es sei denn, die Nachricht enthält bestimmte Begriffe im Betreff.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. Beachten Sie, dass das Codebeispiel davon ausgeht, dass es bereits einen Ordner namens „Dan“ unter dem Posteingang gibt.Note that the code sample assumes that there already exists a folder named "Dan" under the Inbox.

Im Folgenden werden die zum Erstellen der Regel verwendeten Schritte beschrieben:The following describes the steps used to create the rule:

  1. Specify the target folder oMoveTarget to move specific messages as determined by the condition and exception condition.Specify the target folder oMoveTarget to 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.The target folder is a subfolder named "Dan" under the Inbox, and is assumed to already exist.

  2. Verwenden Sie Store. getrules , um einen Satz aller Regeln in der aktuellen Sitzung zu erhalten.Use Store.GetRules to obtain a set of all the rules in the current session.

  3. Verwenden Sie zum Hinzufügen einer neuen Regel mithilfe der Rules -Auflistung, die vom letzten Schritt zurückgegeben wurde, Rules. Create .Using the Rules collection returned from the last step, use Rules.Create to add a new rule. Die neue Regel legt einige Aktionen nach dem Eingang einer Nachricht fest, sodass sie vom Typ olRuleReceive ist.The new rule specifies some action upon receiving a message, so it is of type olRuleReceive.

  4. Verwenden Sie das rule -Objekt, das vom letzten Schritt zurückgegeben wurde, mit der RuleConditions. from -Eigenschaft oFromCondition, um ein ToOrFromRuleCondition -Objekt zu erhalten.Using the Rule object returned from the last step, use the RuleConditions.From property to obtain a ToOrFromRuleCondition object, oFromCondition. oFromCondition gibt die Bedingung für die Regel an: Wenn eine Nachricht von Dan Wilson stammt.oFromCondition specifies the condition for the rule: when a message is from Dan Wilson.

  5. Verwenden Sie das gleiche rule -Objekt, um ein MoveOrCopyRuleAction -Objekt mithilfe der oMoveRuleAction RuleActions. MoveToFolder -Eigenschaft zu erhalten.Using the same Rule object, use the RuleActions.MoveToFolder property to obtain a MoveOrCopyRuleAction object, oMoveRuleAction. oMoveRuleAction specifies the action for the rule: move the message to the target folder "Dan".oMoveRuleAction specifies the action for the rule: move the message to the target folder "Dan".

  6. Verwenden Sie das gleiche rule -Objekt, um ein TextRuleCondition -Objekt mithilfe der oExceptSubject RuleConditions. Subject -Eigenschaft zu erhalten.Using the same Rule object, use the RuleConditions.Subject property to obtain a TextRuleCondition object, oExceptSubject. oExceptSubject specifies 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".oExceptSubject specifies 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".

  7. Verwenden Sie Rules. Save , um die neue Regel zusammen mit den restlichen Regeln für den aktuellen Speicher zu speichern.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 und FeedbackSupport and feedback

Haben Sie Fragen oder Feedback zu Office VBA oder zu dieser Dokumentation?Have questions or feedback about Office VBA or this documentation? Unter Office VBA-Support und Feedback finden Sie Hilfestellung zu den Möglichkeiten, wie Sie Support erhalten und Feedback abgeben können.Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.