Using C#, MAPI on Outlook 2013 email system. Trying to sort the emails in the folder but Folder.Items.Sort("Subject",False) does not work

Wart Pendragon 0 Reputation points
2024-04-17T16:57:14.41+00:00

Using C#, MAPI trying to sort a mail folder by Subject. The output from the below code shows that the Outlook Folder Items are not sorted by Subject. The order is not changed with or without a call to Items.Sort.

NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");

            outlookNamespace.Logon();

            MAPIFolder OLAccount = outlookNamespace.Folders[sAccountName];

            MAPIFolder OLFolder = OLAccount.Folders[sFolderName];

            OLFolder.Items.Sort("Subject",false);

// Access each supposedly sorted mail item. Makes no difference if the sort order is true or false

for(int iMailCount = 1; iMailCount < OLFolder.Items.Count; iMailCount +=1)

            {

                Console.WriteLine($"{iMailCount} -  {OLFolder.Items[iMailCount].Subject}");

            }

//The mail items are not sorted

//Tried this with same result - not sorted

foreach (MailItem eMail in OLFolder.Items)

{

Console.Write($". {eMail.Subject}")

}

Any thoughts?

Thanks

Outlook | Windows | Classic Outlook for Windows | For business
Developer technologies | C#
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.