Create rule with array (in Outlook)

Viewed 51

When I receive e-mail I need check mail body. And I have keywords-more than thousand- If mail has that words, rules should be move to a specific folder. I use this script:

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
    Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
    Set oMoveTarget = oInbox.Folders("test").Folders("subTest")
     
      Set colRules = Application.Session.DefaultStore.GetRules()
    Set oRule = colRules.Create("ruleTest", olRuleReceive)

    Set oMoveRuleAction = oRule.Actions.MoveToFolder
    With oMoveRuleAction
        .Enabled = True
        .Folder = oMoveTarget
    End With
 
    Set oExceptSubject = _
        oRule.Conditions.BodyOrSubject
    With oExceptSubject
        .Enabled = True
        .Text = Array("my keywords")
    End with

colRules.Save
End Sub

But this script dont accept all my keywords, how can I add them in rule...

1 Answers

You need to split the array with commas as the following sample shows:

'Add the subjects to the rule condition
    For Each objRuleCondition In objSpecificRule.Conditions
        If objRuleCondition.ConditionType = olConditionSubject Then
           If objRuleCondition.Enabled = True Then
              Set objTextRuleCondition = objRuleCondition
              strTextArray = objTextRuleCondition.Text
              varSubjects = objDictionary.Keys
  
              varNewTextArray = Split(Join(strTextArray, ",") & "," & Join(varSubjects, ","), ",")
 
              objTextRuleCondition.Text = varNewTextArray
           End If
        End If
    Next

See How to Quickly Add the Subjects of Multiple Emails to the Condition of a Specific Outlook Rule for more information.

Related