How to Get Notified When an Email Arrives in Outlook VBA

In order to do a specific job when a new email arrives into our inbox, we need a VBA function to be triggered. This article focuses on achieving that.

Working with Outlook Events

Outlook Message Arrival OptionsA lot of Outlook applications require some action to be performed when a new email arrives into the inbox. Sometimes this requirement is to automatically save the attachments on a disk drive and at times uploading the email to some 3rd party web server. Outlook provides a number of events to work with, among them is an “Items_ItemAdd” event, which gets triggered whenever a new object (MailItem, AppointmentItem or ContactItem) is added to a specific Outlook folder (Inbox, contacts or calendar). This event needs to be hooked up with some folder in order to set up a triggering point. In the “Application_Startup” routine, we get hold of the current Outlook Session and obtain an instance of the Default Inbox folder. Next we save it to a global variable and set up the event.

Outlook VBA Script

Below is the complete Outlook VBA script:

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
         Dim objApp As Outlook.Application
         Dim objNameSpace As Outlook.NameSpace

         '        Get the items in the Inbox folder
         Set objApp = Outlook.Application
         Set objNameSpace = objApp.GetNamespace("MAPI")
         Set Items = objNameSpace.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal objItem As Object)
         On Error GoTo ShowError
        
         Dim objMail As Outlook.MailItem
        
         '        Check if the item is a mail. If yes, then perform the required task
         If TypeName(objItem) = "MailItem" Then
                   Set objMail = objItem
                   '        Do something here
         End If    

         Exit Sub       

ShowError:
         MsgBox Err.Number & " - " & Err.Description
End Sub

How to Run the Script

Open the Outlook editor by pressing Alt + F11 and paste the following 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. Next save and run “Application_Startup” routine. Now your Outlook code is ready and listening for incoming emails. As soon as an email arrives in the inbox, routine “Items_ItemAdd” will be triggered. It is better to know that this function shall be triggered on every incoming object to your inbox folder. As the only object which is expected to enter the inbox is an email, it is safe to proceed with it. In order to make the script foolproof, a check has been added to ensure that the object type is “MailItem” and not anything else. The script can be tailored for any Outlook default or non-default folder by replacing “olFolderInbox” with any other predefined constant or by obtaining an instance of a user defined folder. It must be remembered that, in case an Outlook folder containing items other than email is hooked up, then the subsequent check of “MailItem” in “Items_ItemAdd” routine should be updated too.

Do the Customized Action

“Do something here” comment in the “Items_ItemAdd” routine is the place where user performs the required action when email arrives. It is vital to know that until the action is being performed, the routine shall not be triggered again, in case another email arrives. Therefore the performed action should be quick and release the resources for next call.

Fixing the Corrupted Outlook Files

Repair Corrupt Outlook DataUnder extensive use, any application can breakdown. Outlook like any other application has an equal chance of failure. Finding yourself in such a case, it is not difficult to recover your important emails if you get your hands on an effective tool to repair corrupt Outlook data.

Author Introduction:

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

Comments are closed.