VBA – How to check messages for specific words before sending them with Outlook

Gmail has the option to revert a message for about 10 seconds after you have sent it:

Well, Outlook with VBA can do similar things, if you are willing to program it a bit with VBA. Let’s say that you are a bad person, who drinks beers and once you drink more than 5, you start sending unpleasant emails to everyone in your mailbox… There are people like this, indeed! 🙂

While you are sober, you may consider “protecting” yourself from something like this, by introducing an Application_ItemSend event in your own Outlook. It would check every email you send for specific words, and if they are present, it would not allow you to send it. Like this:

The code below checks for the words in the forbiddenWords  array and if it finds anything which is similar to them, it sends the Critical error message and does not allow the email to be sent:

Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    
    Dim forbiddenWords As Variant
    Dim word As Variant
    forbiddenWords = Array("stupid", "bad", "nasty")
    
    For Each word In forbiddenWords
        Select Case True
        
            Case LCase(Item.Subject) Like "*" & word & "*"
                Cancel = True
                MsgBox "Please, do not swear in the subject!", vbCritical, "Mail Not Sent"
                Exit Sub
                
            Case LCase(Item.Body) Like "*" & word & "*"
                Cancel = True
                MsgBox "Please, do not swear in the body!", vbCritical, "Mail Not Sent"
                Exit Sub
                
            Case Else
                'ok
                
        End Select
    Next
    
    Select Case MsgBox("Are you sure you want to send this e-mail?", vbYesNo, "Sure?")
    
        Case vbYes
            'ok
            
        Case vbNo
            Cancel = True
            MsgBox "Email is not sent!", vbCritical, "Mail Not Sent"
            
    End Select
    
End Sub

At the end, you get a message, asking you to confirm whether you are willing to send this email or not, although none of the bad words was found. In case that no is selected for an answer, the email is not sent.

If you have noticed that MsgBox  is used once with parenthesis and once without and you realized why this is so, you may pat yourself on the shoulder! Good job!

For everyone else, keep in mind that the MsgBox with parenthesis is a function, which returns a value, equal to vbYes  or vbNo in the second Select Case part of the code.

Cheers!