title: VBA outlook new mail
body: <p>I am trying to run a function every time a new mail arrives in outlook. I have been doing some searching but I am unable to find I way to fire code every time an email arrives. Is there a new mail event that I could utilize? </p>
  
  <p>I added a simple <code>MsgBox</code> to it to be able to see if the event is firing but it did not seem to be working. I placed this code in the <code>ThisOutlookSession</code> module. Any adivice? Here is my code.  </p>
  
  <pre><code>   Public WithEvents myOlApp As Outlook.Application
  
      Sub Initialize_handler()
          Set myOlApp = CreateObject("Outlook.Application")
      End Sub
  
      Private Sub myOlApp_NewMail()
          Dim myExplorers As Outlook.Explorers
          Dim myFolder As Outlook.MAPIFolder
          Dim x As Integer
          Set myExplorers = myOlApp.Explorers
          Set myFolder = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
          If myExplorers.Count &lt;&gt; 0 Then
              For x = 1 To myExplorers.Count
                  On Error GoTo skipif
                  If myExplorers.Item(x).CurrentFolder.Name = "Inbox" Then
                      MsgBox ("Test")
                      myExplorers.Item(x).Display
                      myExplorers.Item(x).Activate
                      Exit Sub
                  End If
      skipif:
              Next x
           End If
           On Error GoTo 0
           myFolder.Display
      End Sub
  </code></pre>