Results 1 to 2 of 2

Thread: Outlook - Need to create a multiple/nested sort

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Outlook - Need to create a multiple/nested sort

    Afternoon all.

    Been about 10 years since I last posted here for help, but here goes

    I am creating a small snippet of code, that will sort my Inbox by sender. The view is that it will delete anythign other than the latest email in a given conversation. My goal is to reduce the number of ermails in my Inbox!!
    I've nearly got it working, but need to sort by [Received] within [From], so I get the latest ones first. I don't know how to do that bit. I can do it manually, by holding down the Shift key, but that doesn't help me programatically, apart from letting me know it is somehow possible!

    Can anyone help ?

    Here is my code if you fancy a laugh (its been a long time since I coded anything!) ...

    Code:
    Sub DeleteOldMessages()
     Dim olItems As Outlook.Items
     Dim SubjectHeading As String
     Dim i As Long, j As Long
     
     ' Default folder to Inbox
     Set olItems = Session.GetDefaultFolder(olFolderInbox).Items
     
     ' Sort by sender
     olItems.Sort "[From]", True
     ' *** need to sort by time within sender, in order to get latest ones first.
     
     
     SubjectHeading = Trim$(olItems(1).Subject)
     For i = 2 To olItems.Count
         If Trim$(olItems(i).Subject) = SubjectHeading Then
            For j = olItems.Count To i + 1 Step -1
                If InStr(1, olItems(j).Subject, SubjectHeading) Then
                    olItems.Item(j).Delete
                End If
            Next
         Else
            SubjectHeading = Trim$(olItems(i).Subject)
         End If
     Next i
     
     Set olItems = Nothing
     End Sub
    Thanks all

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Outlook - Need to create a multiple/nested sort

    i would believe you could do this with a sql query against the inbox
    "select * from olitems where [From] = '" & sender & "' order by [receivedate]"

    i do not have outlook installed so some fixing is probably required, including the correct field name for receivedate, and maybe reverse order for same
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width