How to setup rule for emails that contains specific words in top email body only?

1k Views Asked by At

I created a rule that moves emails that contains a specific word in body to another folder.

It applies whenever that word is in body of older emails down the thread (when you scroll down to older emails that were replied to).

I need it to identify that word within the most recent email body only (and disregard the rest of the thread).

2

There are 2 best solutions below

0
On

I was able to do this by limiting the search area to whatever is above the email header of the second email in the thread.

enter code here
Sub CheckTopBodyWords(olItem As Outlook.MailItem)
    Dim strBody As String
    Dim searchWords As String
    Dim found As Boolean

    searchWords = "WORD" ' Replace with your specific words, separated by a pipe (|) symbol

    strBody = GetTextAboveHeader(olItem.Body)
    found = False

If InStr(1, strBody, searchWords, vbTextCompare) > 0 Then
    found = True
End If

If found Then
    ' Replace "Your Folder Name" with the name of your desired folder.
    olItem.Move Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("TEST")
End If
End Sub

Function GetTextAboveHeader(fullBody As String) As String
    Dim emailHeaderPatterns As Variant
    emailHeaderPatterns = Array("To:", "From:", "Subject:", "Date:") 
    ' Add more header patterns as needed
    Dim foundHeader As Boolean
    foundHeader = False
    Dim result As String
    result = ""

Dim lines As Variant
lines = Split(fullBody, vbCrLf)

Dim line As Variant
For Each line In lines
    If Not foundHeader Then
        Dim headerPattern As Variant
        For Each headerPattern In emailHeaderPatterns
            If LCase(Left(line, Len(headerPattern))) = LCase(headerPattern) Then
                foundHeader = True
                Exit For
            End If
        Next headerPattern
    End If
    
    If foundHeader Then
        Exit For
    Else
        result = result & line & vbCrLf
    End If
Next line

GetTextAboveHeader = result
End Function

Function RegExpTest(str As String, pattern As String) As Boolean
    Dim regEx As Object
    Set regEx = CreateObject("VBScript.RegExp")

    regEx.pattern = pattern
    regEx.IgnoreCase = True
    regEx.Global = True

    RegExpTest = regEx.Test(str)
End Function
0
On

Outlook doesn't distinguish old email bodies and new ones. The message body is a single string. The best what you can do is to compare the message body of two items from the same conversation and extract the newer part. So, following that you will be able to recognize whether a keyword is a part of the newer message or not. The GetConversation method obtains a Conversation object that represents the conversation to which this item belongs. A conversation represents one or more items in one or more folders and stores.

Use the Find/FindNext or Restrict methods of the Items class to find items that correspond to the specified condition. Read more about them with code examples in the following articles:

Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:

  • The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
  • Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
  • You can stop the search process at any moment using the Stop method of the Search class.

Read more about that in the Advanced search in Outlook programmatically: C#, VB.NET article.