0

So, I am currently working on a script that takes emails and processes them in a certain way. I want it to work fully automatically without any human interaction, for the most part I have done this.

The issue I am having is if the mailbox receives an email whilst outlook is closed, I can not make the program kick off because of that email. If I use the

Private Sub Application_Startup() 'Runs at application start
    Call ProcessCode
End Sub

Then it runs too early and the message hasn't been received yet so can't be processed.

I have tried

Private Sub Application_NewMail() 'Runs whenever a new mail item is recieved
    Call ProcessCode
End Sub

But for some reason this doesn't run in this scenario (runs when mail is received normally)

I have also tried Application_ItemLoad, and Application_MAPILogonCompletebut neither of these seemed to work.

What I ideally want is to run the code whenever a unread mail item is present in the mail box, regardless of how it got there.

Thanks in advance

Alex Spicer

Alex Spicer
  • 43
  • 1
  • 11
  • how about adding in a delay/sleep/application.ontime in Application_Startup before the call? – tea_pea Jun 25 '15 at 11:18
  • @MissPalmer tried `application.ontime` and for some reason it returned "Object doesn't support this property or method" – Alex Spicer Jun 25 '15 at 13:15
  • ah, apparently ontime doesn't work in outlook. sorry. saw this, though looks quite extravagant for a work around - http://p2p.wrox.com/book-professional-outlook-2007-programming-isbn-978-0-470-04994-5/82322-ontime-method-outlook.html – tea_pea Jun 25 '15 at 13:26

1 Answers1

0

You may consider using a timer for checking unread emails periodycally. See Outlook VBA - Run a code every half an hour for more information. In the timer's tick event you can get all unread email using the Find/FindNext or Restrict methods of the Items class with the "[UnRead] = true" criteria. Read more about these methods in the following articles:

Also I'd suggest handling the NewMailEx event instead of the NewMail one.

This NewMailEx event is fired once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Note that this behavior has changed from earlier versions of the event when the EntryIDCollection contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired.

The NewMailEx event is fired when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.

Community
  • 1
  • 1
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45