//
type here
我需要按主题升序对特定文件夹中的电子邮件进行排序。对文件夹使用排序不会对项目进行排序。
//To confirm this I have outputted the email Subject values to an Excel file and tried ascending, descending and no sorts but nothing makes a difference in the output order.
这是代码和输出的子集,以显示缺乏排序。 //C#代码 使用 Microsoft.Office.Interop.Outlook; 使用 System.Runtime.InteropServices; 使用 Excel = Microsoft.Office.Interop.Excel;
namespace DictionaryCollection_test_bed
{//Namespace
class Program
{//Program
static void Main(string[] args)
{//Main
//Open an Excel app to enter Outlook emails in the oprder they
//were processed
Excel.Application xlTest = new Excel.Application();
Excel.Workbook xlWorkbookTest = xlTest.Workbooks.Open(sFilePath + "\\SepticTest.xlsx" );
Excel._Worksheet xlWorksheetTest = xlWorkbookTest.Sheets[1]; //
//Open the Outlook file
Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
// Log in to Outlook
NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
outlookNamespace.Logon();
MAPIFolder OLAccount = outlookNamespace.Folders[sAccountName];
MAPIFolder OLFolder = OLAccount.Folders[sFolderName];
OLFolder.Items.Sort("[Subject]", false);//It makes no difference whether the secod
//param is true or false
//or Sort is omitted
Try
{//Try
int iRowCount += 1;
foreach (MailItem eMail in OLFolder.Items)
{
xlWorksheetTest.Cells[iRowCount, 1] = eMail.Subject; //Output to the test workseet
iRowCount += 1;
}
}//Try
{//Catch
catch (System.Exception ex) //when (ex.Message.Contains("(E_NOINTERFACE)") == false)
{//Catch
Finally
{//Finally
xlWorkbookTest.Save();
xlWorkbookTest.Close();
xlTest.Workbooks.Close();
outlookNamespace.Logoff();
System.Runtime.InteropServices.Marshal.ReleaseComObject(outlookNamespace);
System.Runtime.InteropServices.Marshal.ReleaseComObject(outlookApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorksheetTest);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbookTest);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTest);
}//Finally
}//Main
}//Program
}//Namespace
//Output sorted ascending, descending or not sorted
FW: 57-4-2, 2620 EDENTON RD, , 2000 gal, 10/13/2023
FW: 57-5-2, 379 STREET RD, , 1750 gal, 10/18/2023
FW: 57-7-1.1, 149 CHEEK RD, , 1250 gal, 9/12/2023
FW: 57-7-22.2A, 130 WINDEMERE LA, , 1750 gal, 9/13/2023
FW: 57-8-23.2A, 170 SHERROCKEE LA, , 3000 gal, 9/14/2023
FW: APPROVED - HD - Sewage Permit, 10/12/202357-3-3, 5260 HOMEVILLE RD,
FW: 57-8-2.2, 409 PUSEY MILL RD, , 1250 gal, 10/18/2023
您正在对一个集合进行排序,并显示一个完全不同的集合的元素 - 每次调用
MAPIFolder.Items
时,您都会得到一个全新的 Items
集合,该集合不知道该对象的任何其他实例。
将
MAPIFolder.Items
存储在专用变量中,对其进行排序,然后循环遍历它。
您当前的代码(编译器将其翻译成的代码)是
var items1 = OLFolder.Items;
items1.Sort("Subject",false);
var items2 = OLFolder.Items
for(int iMailCount = 1;iMailCount < items2.Count;iMailCount+=1)
{
var items3 = OLFolder.Items;
Console.WriteLine(iMailCount.ToString() +" " + items3[iMailCount].Subject);
}
item1
、items2
和 item3
都是 Items 集合的单独实例。他们不了解其他实例 - 它们是如何排序的,等等。
您的代码必须是
var items = OLFolder.Items;
items.Sort("Subject",false);
for(int iMailCount = 1;iMailCount < items.Count;iMailCount+=1)
{
Console.WriteLine(iMailCount.ToString() +" " + items[iMailCount].Subject);
}