How do I trigger a macro to run after a new mail is received in Outlook? How do I trigger a macro to run after a new mail is received in Outlook? vba vba

How do I trigger a macro to run after a new mail is received in Outlook?


This code will add an event listener to the default local Inbox, then take some action on incoming emails. You need to add that action in the code below.

Private WithEvents Items As Outlook.Items Private Sub Application_Startup()   Dim olApp As Outlook.Application   Dim objNS As Outlook.NameSpace   Set olApp = Outlook.Application   Set objNS = olApp.GetNamespace("MAPI")   ' default local Inbox  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items End SubPrivate Sub Items_ItemAdd(ByVal item As Object)   On Error Goto ErrorHandler   Dim Msg As Outlook.MailItem   If TypeName(item) = "MailItem" Then    Set Msg = item     ' ******************    ' do something here    ' ******************  End IfProgramExit:   Exit SubErrorHandler:   MsgBox Err.Number & " - " & Err.Description   Resume ProgramExit End Sub

After pasting the code in ThisOutlookSession module, you must restart Outlook.


Try something like this inside ThisOutlookSession:

Private Sub Application_NewMail()    Call Your_main_macroEnd Sub

My outlook vba just fired when I received an email and had that application event open.

Edit: I just tested a hello world msg box and it ran after being called in the application_newmail event when an email was received.