Forum Discussion

howie_'s avatar
howie_
Contributor
13 years ago

Sorting Outlook email messages

Hi, 



I've got a piece of code that is intended to retrieve the newest email message from Outlook.  I'm trying to use the Sort() function, as implemented below, but it doesn't seem to be working as expected. The GetFirst() and Item(1) functions both return an email message that are nowhere near the most recent. Can anyone explain how I can implement the Sort() function? Otherwise I'm stuck iterating through all 600+ message items in the inbox searching for the right one. 



var OutlookApplication = Sys.OleObject("Outlook.Application");

var NamespaceMAPI = OutlookApplication.GetNamespace("MAPI");



NamespaceMAPI.Folders(testAccount).Folders("Inbox").Items.Sort("Received", true);



var firstItem = NamespaceMAPI.Folders(testAccount).Folders("Inbox").Items.GetFirst();

var indexOneItem = NamespaceMAPI.Folders(testAccount).Folders("Inbox").Items.Item(1);



Thanks! 

1 Reply

  • Hello, 



    For anyone that is interested, I was able to use the following work-around:



    var inbox = NamespaceMAPI.Folders(testAccount).Folders("Inbox");

    var emailItems = inbox.Items.Restrict("[Subject] = " + emailSubject);

    var item = emailItems(1);





    Items.Restrict() accepts a search query, and is explained in more detail here. Restrict() returns a collection, with 1 being the first index (rather than 0). 



    It doesn't sort my email, but it does help me search for the email in question. 

    I hope this helps.