Sorting Emails by Received time before processing C# Outlook
I need to sort my emails by Received time before processing them as I am processing emails and entering data from it into a database.
I need it so the newest email to be received gets put into the database to overwrite the older version (If there is an older version).
Microsoft.Office.Interop.Outlook.Items item = (Outlook.Items)source.Items;
Source is the folder with the emails in it that I wanted sorted
I have tried these four ways:
items.Sort("ReceivedTime", false);
items.Sort("[ReceivedTime]", Outlook.OlSort开发者_如何学JAVAOrder.olAscending);
items.Sort("ReceivedTime", Outlook.OlSortOrder.olSortNone);
items.Sort("[ReceivedTime]");
Which does not seem to sort it as It still puts the oldest into the database second, overwriting the newest submission.
Any Ideas?
it should be
items.Sort("[ReceivedTime]", false);
or true
if you want them descending
I spent so much time trying to figure out the same problem.
It appears there is some sort of bug in Microsoft Interop.outlook that if you directly try to sort from folder it does not work at all.
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook._NameSpace ns = app.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
inboxFolder.Items.Sort("[ReceivedTime]", false); foreach (var item in inboxFolder.Items) { // ITEMS ARE NOT SORTED }
To make it work you must copy them in different list and then sort. The example below will work.
Outlook.Application app = new Outlook.Application(); Outlook.NameSpace outlookNs = app.GetNamespace("MAPI"); Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); Outlook.Items myItems = emailFolder.Items; myItems.Sort("[ReceivedTime]", false); foreach (var item in myItems) { var itemObj = item as MailItem; if (itemObj != null) { // This time it will work } }
Now I don't know what class your item-Object is, but maybe the "Sort"-Method does not have return type "void", but it returns a new list itself.
So you should assign your list like so:
items = items.Sort();
You could then try, which of your four approaches fits your needs. I hope this helps!
Sub SortByDueDate()
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myItem As Outlook.TaskItem
Dim myItems As Outlook.Items
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderTasks)
Set myItems = myFolder.Items
myItems.Sort "[DueDate]", False
For Each myItem In myItems
MsgBox myItem.Subject &; "-- " &; myItem.DueDate
Next myItem
End Sub
This code is come from MSDN. I am confused as to why at beginning it sets myItmes=myFolder.Items
. After trying many times, I know this is a trap. If you use myFolder.Items.sort
... directly, the sort function does not work.
精彩评论