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?
 
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

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

Chris
Melbourne-Australia
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.