次の方法で共有


Outlook Programming Series # 14 : How to - Create a Rule to Move Specific E-mails to a Folder programmatically ?

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 "Outlook" under the Inbox.

Code snippet:

    1:  Sub CreateRule()
    2:      Dim colRules As Outlook.Rules
    3:      Dim oRule As Outlook.Rule
    4:      Dim colRuleActions As Outlook.RuleActions
    5:      Dim oMoveRuleAction As Outlook.MoveOrCopyRuleAction
    6:      Dim oFromCondition As Outlook.ToOrFromRuleCondition
    7:      Dim oExceptSubject As Outlook.TextRuleCondition
    8:      Dim oInbox As Outlook.Folder
    9:      Dim oMoveTarget As Outlook.Folder
   10:   
   11:      'Specify target folder for rule move action
   12:      Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
   13:      'Assume that target folder already exists
   14:      Set oMoveTarget = oInbox.Folders("Outlook")
   15:      
   16:      'Get Rules from Session.DefaultStore object
   17:      Set colRules = Application.Session.DefaultStore.GetRules()
   18:      
   19:      'Create the rule by adding a Receive Rule to Rules collection
   20:      Set oRule = colRules.Create("Sample_rule", olRuleReceive)
   21:   
   22:      'Specify the condition in a ToOrFromRuleCondition object
   23:      'Condition is if the message is from "<alias>"
   24:      Set oFromCondition = oRule.Conditions.From
   25:      With oFromCondition
   26:          .Enabled = True
   27:          .Recipients.Add ("<alias>")
   28:          .Recipients.ResolveAll
   29:      End With
   30:   
   31:      'Specify the action in a MoveOrCopyRuleAction object
   32:      'Action is to move the message to the target folder
   33:      Set oMoveRuleAction = oRule.Actions.MoveToFolder
   34:      With oMoveRuleAction
   35:          .Enabled = True
   36:          .Folder = oMoveTarget
   37:      End With
   38:   
   39:      'Specify the exception condition for the subject in a TextRuleCondition object
   40:      'Exception condition is if the subject contains "spam" or "text"
   41:      Set oExceptSubject = _
   42:          oRule.Exceptions.Subject
   43:      With oExceptSubject
   44:          .Enabled = True
   45:          .Text = Array("spam", "text")
   46:      End With
   47:   
   48:      'Update the server and display progress dialog
   49:      colRules.Save
   50:  End Sub
  
 Reference: For more detailed information, please go through this article.