Using Outlook VBA, learn how to restrict an email being sent out from the user’s Outlook account if specific words are contained in the email body or subject.
Identify the Outlook Event to Block the Emails
There is often a very pressing requirement of not letting the email sent out from the user’s account, if a specific criterion is met. To be more precise, in large organizations, where data confidentiality is an important concern, the organization might want to block the outgoing emails if some specific words are found in email body or subject, or if the email contains any attachments, as these might be in violation of their data confidentiality policy. Outlook makes us available a set of events which allow us to take control of Outlook email handling procedures. “Application_ItemSend” is the event which can allow us to tackle the above problem. This routine is called each time anything is sent out from the user’s Outlook account to another user. It can be an email, appointment or a calendar entry. In event parameters, the item being sent is passed as an object, along with a Boolean flag which allows or blocks the sending of the object from client to server. In this example, we are going to check if the email being sent contains the word “confidential” in its subject line. If so, it shall not be allowed to leave Outlook.
Outlook VBA Script
Below is the complete Outlook VBA script:
Public WithEvents objApp As Outlook.Application Public Sub Initialize_handler() Set objApp = Outlook.Application End Sub Private Sub objApp_ItemSend(ByVal objItem As Object, Cancel As Boolean) Dim objMail As Outlook.MailItem If TypeName(objItem) = "MailItem" Then Set objMail = objItem ' Check if the email subject containing the specific word If InStr(1, objMail.Subject, "confidential") > 0 Then Cancel = True Exit Sub End If End If End Sub
How to Execute the Script
Press Alt + F11 to open the Outlook VBA editor and paste the above code in “ThisOutlookSession”. It is important to know that prior to running this script, “Microsoft Outlook XX Object library” should be added to the project references from “Tools” menu, where XX is the library version.
Understand the Script
In the script, “Initialize_handler” routine will hook up the “objApp_ItemSend” event, making sure that this event is called whenever Outlook attempts to send something out. As soon as the user presses the send button, the script will first check if the item being sent is a “MailItem”. If found so, the script will check if the email subject contains the word ”confidential” and set “Cancel” to “True”, to tell Outlook that the send operation should be cancelled. It is a minor but critical point to observe that the inbuilt function “InStr” is case sensitive, so it will only look for word “confidential” in lower case as written above. We can use other standard string operations to check for any word irrespective of case sensitivity. Similarly as this script looks for words in Email subject, it can also be made to look for words in email body or other parameters, as required.
Recover Outlook Data
If your Outlook has crashed and you have lost your emails and calendar appointments, it’s a good time to get hold of a powerful tool to fix damaged Outlook. It will bring back all the emails and other Outlook items without failing.
Mary Underwood is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including dwg recovery and rar recovery software products. For more information visit www.datanumen.com