簡體   English   中英

優化代碼以在Outlook加載項中搜索電子郵件

[英]Optimize code for searching emails in Outlook Add-in

我擁有將特定電子郵件信息打印到Excel文件的下一個解決方案。 我選擇在該日期之前必須寫的電子郵件。

    [STAThread]
    public void Summary(DateTime startDate, DateTime finishDate, string fileSaveAdress)
    {
        try
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook.NameSpace oNs = oApp.GetNamespace("mapi");
            oNs.Logon(Missing.Value, Missing.Value, false, false);

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(Missing.Value);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            Outlook.MAPIFolder pickedFolder = oApp.Session.PickFolder();
            Outlook.Items pickedFolderItems = pickedFolder.Items;

            int iX = 1;
            int iY = 1;
            int iAdjustColomn = 0;

            foreach (object obj in pickedFolderItems)
            {
                if (obj is Outlook.MailItem)
                {
                    Outlook.MailItem oMsg = (Outlook.MailItem)obj;
                    if (oMsg.ReceivedTime.ToUniversalTime() > startDate.ToUniversalTime() &&
                        oMsg.ReceivedTime.ToUniversalTime() < finishDate.ToUniversalTime())
                    {
                        xlWorkSheet.Cells[iY, iX] = oMsg.ReceivedTime.ToShortDateString() + " " +
                                                    oMsg.ReceivedTime.ToShortTimeString();
                        xlWorkSheet.Cells[iY, ++iX] = oMsg.Sender.Name;

                        for (int i = 1; i <= oMsg.Recipients.Count; i++)
                        {
                            xlWorkSheet.Cells[iY, ++iX] = oMsg.Recipients[i].Name;
                        }

                        xlWorkSheet.Cells[iY, ++iX] = oMsg.Subject;

                        Outlook.Attachments AttachmentArray = oMsg.Attachments;
                        if (AttachmentArray.Count != 0)
                        {
                            foreach (Outlook.Attachment attachment in AttachmentArray)
                            {
                                xlWorkSheet.Cells[iY, ++iX] = attachment.DisplayName;
                            }
                        }

                        iAdjustColomn += iX;

                        iY += 2;
                        iX = 1;
                    }
                }
            }

            for (int i = 1; i < iAdjustColomn; i++)
            {
                AutoFitColumn(xlWorkSheet, i);
            }

            xlWorkBook.SaveAs(fileSaveAdress, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                              Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            xlWorkBook.Close(true, Missing.Value, Missing.Value);
            xlApp.Quit();

            oNs.Logoff();
        }
        catch (Exception e)
        {
            ErrorReport er = new ErrorReport(e.Message);
            er.ShowDialog();
        }
    }

而且主要的問題是它給出了一個錯誤,該錯誤沒有足夠的內存來工作。 如何優化呢? 請幫忙!

我將使用計數器將使用Outlook對象的foreach調用替換為for循環。 使用foreach通常是對Outlook數據的不良做法。

另外,一種更快的方法是將Table對象與Folder.GetTable一起使用,在其中您可以設置要為迭代檢索的最小列數/字段數,以最大程度地減少內存使用量。

Microsoft的示例( http://msdn.microsoft.com/zh-cn/library/bb176423%28v=office.12%29.aspx ):

Sub RemoveAllAndAddColumns()
'Declarations
Dim Filter As String
Dim oRow As Outlook.Row
Dim oTable As Outlook.Table
Dim oFolder As Outlook.Folder

'Get a Folder object for the Inbox
Set oFolder = Application.Session.GetDefaultFolder(olFolderInbox)

'Define Filter to obtain items last modified after May 1, 2005
Filter = "[LastModificationTime] > '5/1/2005'"
'Restrict with Filter
Set oTable = oFolder.GetTable(Filter)

'Remove all columns in the default column set
oTable.Columns.RemoveAll
'Specify desired properties
With oTable.Columns
    .Add ("Subject")
    .Add ("LastModificationTime")
    'PR_ATTR_HIDDEN referenced by the MAPI proptag namespace
    .Add ("http://schemas.microsoft.com/mapi/proptag/0x10F4000B")
End With

'Enumerate the table using test for EndOfTable
Do Until (oTable.EndOfTable)
    Set oRow = oTable.GetNextRow()
    Debug.Print (oRow("Subject"))
    Debug.Print (oRow("LastModificationTime"))
    Debug.Print (oRow("http://schemas.microsoft.com/mapi/proptag/0x10F4000B"))
Loop
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM