How to Auto Block the Incoming Emails Whose Bodies Contain All Specific Keywords via Outlook VBA

With an Outlook rule to block new emails with specific keywords in body, as long as an email whose body contains one of the specific keywords, it’ll be blocked. But, if you wish to only block the emails whose body contains all the specific keywords, you have to use other ways, such as the means introduced in this article.

If you’ve disabled the native “anti-spam” feature in Outlook, you may opt to apply Outlook rules to automatically block the suspicious emails. For instance, you can create an Outlook rule to auto move the emails which contain specific keywords, like “test”, “sample”, “example”, in body to “Junk E-mail” folder. Then, if a new email’s body contains one of the specific keywords, such as containing “test” in body, the email will be moved. However, generally, you prefer to only block the emails which contain all the specific keywords in body. To achieve that, you have to utilize the other means, such as the following approach – using VBA code.

Auto Block the Incoming Emails Containing All the Specific Keywords in Body

  1. Firstly, launch Outlook program.
  2. Then, trigger Outlook VBA editor according to “How to Run VBA Code in Your Outlook“.
  3. Next, put the following VBA code into the “ThisOutlookSession” project.
Public WithEvents objInboxFolder As Outlook.Folder
Public WithEvents objInboxItems As Outlook.Items
Public objJunkFolder As Outlook.Folder

Private Sub Application_Startup()
    Set objInboxFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
    Set objInboxItems = objInboxFolder.Items
    Set objJunkFolder = Outlook.Application.Session.GetDefaultFolder(olFolderJunk)
End Sub

Private Sub objInboxItems_ItemAdd(ByVal objItem As Object)
    Dim objMail As Outlook.MailItem
    Dim strBody As String

    If TypeName(objItem) = "MailItem" Then
       Set objMail = objItem
       strBody = LCase(objMail.Body)
 
       'Find specific keywords in body
       If InStr(strBody, "test") > 0 And InStr(strBody, "sample") > 0 Then
 
          objMail.Move objJunkFolder
 
          'objMail.Delete ==> delete it
       End If
    End If
End Sub

VBA Code - Auto Block the Incoming Emails whose Bodies Contain All Specific Keywords

  1. After that, restart Outlook to activate this macro.
  2. Since then, every time when an incoming mail which contains all the specific keywords arrives in inbox, the email will be auto moved to “Junk Email” folder, as shown in the following screenshot.Auto Blocked Email Whose Body Contains All Specific Keywords

Recover Corrupt Outlook Data

When your Outlook file is infected with viruses, the file is very likely to be corrupt. In this case, you have to firstly run antivirus software to scan the file to kill viruses. Then, you can run an experienced and credible PST repair utility, such as DataNumen Outlook Repair, to repair PST file, getting back the compromised Outlook data.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including corrupt SQL Server and outlook repair software products. For more information visit www.datanumen.com

Comments are closed.