none
MailItem Collection Order for a folder

    Question

  • I have an Access 2010 application that is reading from Outlook 2010. The code basically sets an object pointer to a folder in the Outlook mailbox, and then proceeds to look at each message in the folder. However, I have tried every which way to read from the oldest message to the newest message with very sporadic results. Sometimes, in fact, the first item in the collection of mail items will start with an item in the middle of my folder (sorted by date), and move from the middle to the oldest date. Once it hits the oldest message it starts one message above where it initially started and moves to the newest message. This is only a problem when the inbox has more than x items in it, where x is always variable. I might remove some items and then the message it starts on changes.

    Has anyone else experienced a similar problem, or can you find a solution? Again the problem is very sporadic, but did not occur in Outlook/Access 2007. Not sure what the issue might be. . .

    Wednesday, June 22, 2011 8:04 PM

Answers

  • Because you are sorting one object

    OLF.items.sort "[Received]", false

    and using another one

    'MsgBox OLF.Items(i).SentOn

    Each time you call OLF.items, you get a brand new COM obejct

    Change you code to

    Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").Folders("SOME MAILBOX").Folders("Inbox")
    set Items = OLF.Items
    items.sort "[Received]", false
    EmailItemCount = Items.count
    i = EmailItemCount
    While i > 0
     'MsgBox Items(i).SentOn 'Uncomment to show sent on date for each succsesive item in the collection
      i = i - 1
    Wend

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been


    • Marked as answer by mdickneite Thursday, June 23, 2011 2:28 PM
    Thursday, June 23, 2011 1:45 PM

All replies

  • There is no predefined sort order unless you explicilty set it using Items.Sort
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been

    Wednesday, June 22, 2011 8:58 PM
  • I thought that as well, but even after using the following code:

    OLF.items.sort "[Received]", false

    I do not get any difference in results. Am I executing the sort sytax incorrectly, possibly?


    EDIT:

    Here is my code to get the list of items from the inbox:

    Function ScanEmails()
    
     Dim OLF As Outlook.MAPIFolder
    
     Dim EmailItemCount As Integer
    
     Dim i As Integer
    
      
    
     Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").Folders("SOME MAILBOX").Folders("Inbox")
    
     EmailItemCount = OLF.Items.count
    
     i = EmailItemCount
    
      
    
     While i > 0
    
      With OLF.Items(i)
    
       'MsgBox OLF.Items(i).SentOn 'Uncomment to show sent on date for each succsesive item in the collection
    
      End With
    
      i = i - 1
    
     Wend
    
    End Function

    Again, with a varying quanitity of email items in the box the first item in the array will sometimes be right in the middle of my inbox. I took out the sorting code in this code segment, but I've tried adding that in as well with no joy.
     


    Thursday, June 23, 2011 11:44 AM
  • Because you are sorting one object

    OLF.items.sort "[Received]", false

    and using another one

    'MsgBox OLF.Items(i).SentOn

    Each time you call OLF.items, you get a brand new COM obejct

    Change you code to

    Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").Folders("SOME MAILBOX").Folders("Inbox")
    set Items = OLF.Items
    items.sort "[Received]", false
    EmailItemCount = Items.count
    i = EmailItemCount
    While i > 0
     'MsgBox Items(i).SentOn 'Uncomment to show sent on date for each succsesive item in the collection
      i = i - 1
    Wend

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been


    • Marked as answer by mdickneite Thursday, June 23, 2011 2:28 PM
    Thursday, June 23, 2011 1:45 PM
  • That was it exactly. Thank you so much for the assistance!

    Thursday, June 23, 2011 2:28 PM