Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Apr 2002
    Location
    Melbourne, Australia
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing new mail items via VBA (Outlook 2000)

    I've been learning how to use VBA with Outlook 2000 and I've seen some code to examine a new mail message in the Application_NewMail() trigger, but it relies on extracting the first mailitem in the Inbox folder.

    My question is: how synchronous is this process? If I'm in NewMail(), is the first Inbox item ALWAYS the one that I'm handling? Will it stay that way for the life of my procedure? Is there some way to extract a handle to the MailItem in question from the NewMail() environment itself?

    Any and all advice appreciated,

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Accessing new mail items via VBA (Outlook 2000)

    Hi,
    In my (admittedly quite limited) experience, the newmail event is fairly inexact in that it doesn't seem to necessarily fire once for each mailitem if you receive more than one in quick succession. This can mean that the item you process by looking at the first item in the inbox is not necessarily the one that triggered the event initially. This is particularly true if you use messageboxes in the routine that is triggered. I also had problems due to rules processing the messages as they arrived too. Unlike the Item_Send event, the NewMail event doesn't give you any easy way of linking to the particular item.
    If you do find a consistent way around this, I'd love to hear about it as I'm still researching it off and on.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    New Lounger
    Join Date
    Apr 2002
    Location
    Melbourne, Australia
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accessing new mail items via VBA (Outlook 2000)

    Thanks Rory....

    I've been thinking about this some more. How about this as a strategy....

    1. Consider the NewMail event as simply an indication that there are one or more new mail messages.
    2. When handling the event, start at the top mail item and process it. After processing, mark it somehow - find some innocuous property that can be used as a flag ( dunno what yet - like I said.. I'm still new at this!).
    3. Continue to process MORE mail items down the list until finding one that is already marked (ie. I've seen it before)
    4. Then, to ensure we catch any that arrived in the interim, go back to Step 2
    5. Exit when the top item is already marked.

    This does have the disadvantage that unmarked mail items are handled in reverse arrival order (which may or may not matter, considering that they would have arrived almost simultaneously.)

    Ouch!.. I've just thought of something.... If I'm just about to look at the Second item.. and another arrives, does the previous second now instantly become the THIRD???? OK.. a better strategy would then be:

    1. Scan down the list from the top looking for the first marked mail item (say, number 'n').
    2. If that's number 1, exit
    3. Handle mailitem 'n-1' and mark it.
    4. Go back to step 1.

    I reckon this will work, and it'll handle them in the right order. Any ideas on a property that could be used as a flag?

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Accessing new mail items via VBA (Outlook 2000)

    I guess the simplest thing would be to add a new UserProperty to the mailitem. I was going to say that you could use the EntryID of the mailitem, store it in an array and then check new ones against that to see if they've already been processed but it's a slightly unwieldy and very long string. Could be done but there may be easier ways, possibly defining a collection and having the newmail event add the first item in the inbox to it. Again, I suspect there may be timing issues and rules might also interfere.
    I think you're on the right track with the reverse order processing though!
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    New Lounger
    Join Date
    Apr 2002
    Location
    Melbourne, Australia
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accessing new mail items via VBA (Outlook 2000)

    Thanks for the ideas. I like the concept of a UserProperty (something wholly contained within the mailitem itself that only I care about). I gave it a try and got VERY confused for a while (probably my unfamiliarities with collection objects). I finally got something going and tried to look at it in the GUI - ie. I figured that it might match the User Defined fields in the View Customiser.. but it doesn't seem to be the same thing - I filled every mailitem (and my InBox has MANY <sigh>) with a text UserProperty called "Handled" and set it to "Yes", but if I add a User Defined field called Handled of type text to my view, the whole column is blank.

    That's as far as I got... I haven't tried to use it in anger in the manner that I originally intended (I was offline at the time, so new mail would have been rare!). I'll give it a go as soon as I get a chance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •