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

I'm writing a macro that creates tickets on a database based on alerts received from a Nagios server as an email. However, I cannot let the macro run in an infinite loop while checking for mails because it is just too resource heavy and makes my desktop hang. I need to find a way to trigger the macro only when a new mail is received.

I looked for something along the lines of NewMail events on the MSDN website, but I can't find anything coherent. Can anyone show me just a bit of sample code to show how to trigger macros from new mail events?

This question is related to vba outlook triggers

The answer is


Try something like this inside ThisOutlookSession:

Private Sub Application_NewMail()
    Call Your_main_macro
End 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.


Examples related to vba

Copy filtered data to another sheet using VBA Better way to find last used row Check if a value is in an array or not with Excel VBA Creating an Array from a Range in VBA Excel: macro to export worksheet as CSV file without leaving my current Excel sheet VBA: Convert Text to Number What's the difference between "end" and "exit sub" in VBA? Rename Excel Sheet with VBA Macro Extract Data from PDF and Add to Worksheet Quicker way to get all unique values of a column in VBA?

Examples related to outlook

Does VBA contain a comment block syntax? "Sub or Function not defined" when trying to run a VBA script in Outlook Change HTML email body font type and size in VBA css padding is not working in outlook Image style height and width not taken in outlook mails Paste Excel range in Outlook MS Access VBA: Sending an email through Outlook HTML email in outlook table width issue - content is wider than the specified table width Save attachments to a folder and rename them Sending email from Command-line via outlook without having to click send

Examples related to triggers

How to get JQuery.trigger('click'); to initiate a mouse click insert/delete/update trigger in SQL server How do you change Background for a Button MouseOver in WPF? mysql after insert trigger which updates another table's column creating triggers for After Insert, After Update and After Delete in SQL Why is the GETDATE() an invalid identifier Declare variable MySQL trigger How to emulate a BEFORE INSERT trigger in T-SQL / SQL Server for super/subtype (Inheritance) entities? MySql Error: Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger jQuery click anywhere in the page except on 1 div