Outlook Application_NewMailEx not working on startup

喜欢而已 提交于 2019-12-20 02:32:05

问题


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

回答1:


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.




回答2:


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.





回答3:


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.



来源:https://stackoverflow.com/questions/41281091/outlook-application-newmailex-not-working-on-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!