Outlook Application_NewMailEx not working on startup

3.7k views Asked by At

I am using a Application_NewMailEx to treat all emails received. It works fine on emails received while Outlook is open.

However on startup, the Application_NewMailEx does not get called by received emails.

I tried using a Application_Startup but it is called before emails are received ==> does not work. There is no application.ontime to delay the startup macro... Application_NewMail does the same.

How can it be done?

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    INIT_FOLD
    TreatMsg Application.GetNamespace("MAPI").GetItemFromID(EntryIDCollection)
End Sub
3

There are 3 answers

2
Dmitry Streblechenko On BEST ANSWER

NewMailEx event will fire only for the messages received while your code was running. It will not fire for the emails received in your (Exchange?) mailbox before that.

You can either process the unread emails in the Inbox on startup (Items.Restrict or Items.Find/FindNext) assuming that new unprocessed messages are still unread or (in case of cached mode) use Items.ItemAdd event on the Inbox folder - it will fire when your OST file is being synchronized with the remote mailbox.

0
Anderson Rissardi On

Items.ItemAdd and NewMailEx do not work when you have more than 8 items coming in. Microsoft does not guarantee that it will trigger this event itself.

1
0m3r On

Here is an Example on how to setup Application Startup and trigger your vba when MailItem is add to Inbox

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim Inbox  As Outlook.MAPIFolder

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Inbox.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        Example Item ' call sub
    End If
End Sub

Public Sub Example(ByVal Item As Object)
    Debug.Print Item.Subject ' Immediate Window
End Sub

Application.Startup Event (Outlook) and Items.ItemAdd Event (Outlook)

Items.ItemAdd Event (Outlook) Occurs when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once. This event is not available in Microsoft Visual Basic Scripting Edition (VBScript).


Application.Startup Event (Outlook) Occurs when Microsoft Outlook is starting, but after all add-in programs have been loaded.