41

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Gautam Mainkar
  • 546
  • 1
  • 5
  • 9

2 Answers2

59

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 Sub
Private 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 If
ProgramExit: 
  Exit Sub
ErrorHandler: 
  MsgBox Err.Number & " - " & Err.Description 
  Resume ProgramExit 
End Sub

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

JimmyPena
  • 8,694
  • 6
  • 43
  • 64
10

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.

Deanna
  • 23,876
  • 7
  • 71
  • 156
Alistair Weir
  • 1,809
  • 6
  • 26
  • 47
  • I'm afraid it doesn't seem to be working. I tried running the macro and then sending myself a mail, but I couldn't get my macro to run until I reinitialized the whole thing. What do you think the problem could be? – Gautam Mainkar Jun 29 '12 at 15:19
  • Where did you have the code in my answer? I placed it in `ThisOutlookSession` and left it. No need to run the macro it will trigger when a mail is received. – Alistair Weir Jun 29 '12 at 15:37
  • I had it pasted in ThisOutlookSession too, but it's giving me a lot of problems during running. I can't even point to the root of the problem, my sub just does utterly random things... – Gautam Mainkar Jul 02 '12 at 09:41
  • 1
    If you paste something like this in the 'do something here' section: `Call Your_main_macro`, I have found that the macro has to be stored in the same module, `ThisOutlookSession`. If it was in a different module, I got an error. I had to restart Outlook for any changes to take effect. – Philip Day Mar 25 '15 at 11:03