Outlook VBA NewMail Event

Hi,

I am using the NewMail event in Outlook to run a procedure when I receive e-mails depending on the e-mail recieved. The issue I have is that sometimes you recieve more than 1 e-mail at a time and therefore one of the e-mails is not 'dealt' with. Is there anyway to get around this?

Thanks,
mcs26Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris Raisin(Retired Analyst/Programmer)Commented:
Stand by.....

Chris
Melbourne-Australia
Chris Raisin(Retired Analyst/Programmer)Commented:
The problem is that you are using "Newmail" which only runs once whenever any new mail arrives into your Inbox.

To deal with each individual email as it is received you need to use the
"NewMailEx" event.

A full reference to this event is showing at:
http://msdn.microsoft.com/en-us/library/aa171304%28v=office.11%29.aspx

I show some example code I have devised  (based on the code in the link supplied by Microsoft) to show how to handle individual emails as they are received.

The code is placed inside the Outlook object "ThisOutlookSession" within your macro editor in Outlook. This code that I have written will look at each item as it is received and simply display the subject of each email item in a message box
(not very practical, but will let you test it easily to prove it works).

You could adjust this code to selectively pick up email which (perhaps) is from a certain person (using the properties of the email item "mai") and do such things as "file" into a certain folder, or anything else your imagination can conjure up!

I hope this has put you on the right track....

Cheers
Chris


Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
  Dim mai As Object
  Dim intInitial As Integer
  Dim intFinal As Integer
  Dim strEntryId As String
  Dim intLength As Integer
  Dim nCount As Integer
    
  intInitial = 1
  intLength = Len(EntryIDCollection)
  intFinal = InStr(intInitial, EntryIDCollection, ",")
  Do While intFinal <> 0
    strEntryId = Strings.Mid(EntryIDCollection, intInitial, _
                 (intFinal - intInitial))
    Set mai = Application.Session.GetItemFromID(strEntryId)
    nCount = nCount + 1
    MsgBox "Message No: " + CStr(nCount) + vbCrLf + mai.Subject
    intInitial = intFinal + 1
    intFinal = InStr(intInitial, EntryIDCollection, ",")
  Loop
  'process final message in the EntryIdCollection
  strEntryId = Strings.Mid(EntryIDCollection, intInitial, _
               (intLength - intInitial) + 1)
  nCount = nCount + 1
  Set mai = Application.Session.GetItemFromID(strEntryId)
  MsgBox "Message No: " + CStr(nCount) + vbCrLf + mai.Subject
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.