none
Why the sorting result is not correct? RRS feed

  • Question

  • Hi,

    I need to move the earliest mails to another folder. Therefor, I sorted the mails by received time using following code.

    Sub SortTest()
        Dim olApp As New Outlook.Application
        Dim Folder As Outlook.Folder
        Dim nmsName As Outlook.NameSpace
        Dim s As String=""
    
        nmsName = olApp.GetNamespace("MAPI")
        Folder = nmsName.folders("201701")
        Folder.Items.Sort ("[ReceivedTime]", True)
        
        For Each itm In Folder.Items
            If s = "" Then
                s = itm.ReceivedTime
            Else
                s = s & Chr(13) & itm.ReceivedTime
            End If
        Next
        MsgBox(s)
    
    End Sub

    However, the result 's' is look like this, Why?

    s:

    2017/1/13 20:05:48
    2017/1/13 17:49:59
    2017/1/12 15:17:08
    2017/1/11 8:59:29
    2017/1/14 14:12:12
    2017/1/16 8:07:55
    2017/1/15 8:25:47

    Wednesday, January 18, 2017 11:40 AM

Answers

  • Hello Leon,

    You need to get an instance of the Items class and then sort them. So, you can use a sorted collection in the loop. Calling the Items property each time gives you a new instance of the Items class (which is unsorted).

    Sub SortTest()
        Dim olApp As New Outlook.Application
        Dim Folder As Outlook.Folder
        Dim nmsName As Outlook.NameSpace
        Dim myItems As Outlook.Items 
        Dim s As String=""
    
        nmsName = olApp.GetNamespace("MAPI")
        Set Folder = nmsName.folders("201701")
        Set myItems = Folder.Items
        
        myItems.Sort ("[ReceivedTime]", True)
        
        For Each itm In myItems
            If s = "" Then
                s = itm.ReceivedTime
            Else
                s = s & Chr(13) & itm.ReceivedTime
            End If
        Next
        MsgBox(s)
    
    End Sub


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by leon1526 Wednesday, January 18, 2017 12:37 PM
    Wednesday, January 18, 2017 12:16 PM

All replies

  • Hello Leon,

    You need to get an instance of the Items class and then sort them. So, you can use a sorted collection in the loop. Calling the Items property each time gives you a new instance of the Items class (which is unsorted).

    Sub SortTest()
        Dim olApp As New Outlook.Application
        Dim Folder As Outlook.Folder
        Dim nmsName As Outlook.NameSpace
        Dim myItems As Outlook.Items 
        Dim s As String=""
    
        nmsName = olApp.GetNamespace("MAPI")
        Set Folder = nmsName.folders("201701")
        Set myItems = Folder.Items
        
        myItems.Sort ("[ReceivedTime]", True)
        
        For Each itm In myItems
            If s = "" Then
                s = itm.ReceivedTime
            Else
                s = s & Chr(13) & itm.ReceivedTime
            End If
        Next
        MsgBox(s)
    
    End Sub


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by leon1526 Wednesday, January 18, 2017 12:37 PM
    Wednesday, January 18, 2017 12:16 PM
  • Hi Eugene, The result is perfect as per your proposal. Thank you very much!
    Wednesday, January 18, 2017 12:39 PM