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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,260 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
4,895 questions
0 comments No comments
{count} votes