2011-09-05 150 views
2

在處理電子郵件並將數據輸入到數據庫中時,我需要按照收到的時間對郵件進行排序。按照收到的時間對郵件進行排序處理C#Outlook

我需要它,所以最新收到的電子郵件將被放入數據庫中以覆蓋舊版本(如果存在舊版本的話)。

Microsoft.Office.Interop.Outlook.Items item =(Outlook.Items)source.Items;

來源是,我想整理

在它的電子郵件文件夾我已經嘗試了這四種方式:

  items.Sort("ReceivedTime", false); 
      items.Sort("[ReceivedTime]", Outlook.OlSortOrder.olAscending); 
      items.Sort("ReceivedTime", Outlook.OlSortOrder.olSortNone); 
      items.Sort("[ReceivedTime]"); 

似乎這不排序,因爲它仍然把最古老的入數據庫第二,覆蓋最新的提交。

任何想法?

回答

3

應該

items.Sort("[ReceivedTime]", false); 

true如果你想讓他們降

0

現在我不知道你的item-Object是什麼類,但也許「排序」 - 方法沒有返回類型「void」,但它返回一個新的列表本身。

所以,你應該分配您的列表,像這樣:

items = items.Sort(); 

然後,您可以嘗試,其中的四種方法適合您的需求。 我希望這有助於!

+0

Microsoft.Office.Interop.Outlook.Items item =(Outlook.Items)source.Items; 源是我想要排序的電子郵件文件夾。 所以這不會像Sort所帶參數那樣工作。 – Mac

4

我花了這麼多時間試圖找出了同樣的問題。

看來Microsoft Interop.outlook中存在某種錯誤,如果您直接嘗試從文件夾中排序,它根本不起作用。

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 
     } 

爲了使它工作,你必須在不同的列表,然後排序複製它們。下面的例子將起作用。

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 } }
+1

在Items對象的一個​​實例上調用'inboxFolder.Items.Sort',並通過不同對象的項目循環('foreach(inboxFolder.Items中的var item) ')。將inboxFolder.Items的值存儲在一個單獨的變量中,對其調用Sort,然後遍歷其元素。 –

+0

正確。這也是我的想法。 – Manan

0
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 

此代碼來自MSDN。我很困惑,爲什麼在開始時它設置了myItmes=myFolder.Items。經過多次嘗試後,我知道這是一個陷阱。如果直接使用myFolder.Items.sort ...,排序功能不起作用。

+0

請刪除評論的評論,然後才能得到答案? –

相關問題