2011-02-01 53 views
0

我正在嘗試將指定日期的所有任務收集到新電子郵件的正文中。這包括標記爲跟進的電子郵件。如何選擇Outlook中標記爲跟進特定日期的所有電子郵件(C#Outlook AddOn)

以下代碼完成我所需要的操作,但速度很慢。通過所有的電子郵件的迭代是其中代碼浪費時間(childFolder.DefaultItemType == Outlook.OlItemType.olMailItem)很多:

public static void AddTaskList(DateTime tasksDate, Boolean append) 
{ 
    // need to create a variable with the date of 1/1/4501 12:00:00 AM (this counts as the empty date in Outlook) 
    DateTime nonDate = new DateTime(4501, 1, 1); 

    Cursor.Current = Cursors.WaitCursor; 

    Outlook.Application application = Globals.ThisAddIn.Application; 
    Outlook.Inspector inspector = application.ActiveInspector(); 
    Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem; 

    string body = ""; 

    body += "\nExtracted from Outlook tasks (" + tasksDate.ToShortDateString() + "):\n\n"; 

    Outlook.Folder root = application.Session.DefaultStore.GetRootFolder() as Outlook.Folder; 

    // Find the tasks folder and extract all the tasks for the specified day 
    ArrayList taskList = new ArrayList(); 
    foreach (Outlook.Folder childFolder in root.Folders) 
    { 
     if (childFolder.DefaultItemType == Outlook.OlItemType.olTaskItem) 
     { 
      foreach (Outlook.TaskItem item in childFolder.Items) 
      { 
       if (item.ConversationTopic != null && item.DueDate.Equals(tasksDate)) 
       { 
        TaskItem myTask = new TaskItem(); 

        myTask.Subject = item.Subject.ToString(); 
        myTask.ToDoTaskOrdinal = item.ToDoTaskOrdinal; 
        myTask.Complete = item.Complete; 

        taskList.Add(myTask); 
       } 
      } 
     } 
     else if (childFolder.DefaultItemType == Outlook.OlItemType.olMailItem) 
     { 
      foreach (object item in childFolder.Items) 
      { 
       Outlook.MailItem mail = item as Outlook.MailItem; 
       if (mail != null) 
       { 
        if (mail.IsMarkedAsTask && mail.TaskDueDate.Equals(tasksDate)) 
        { 
         TaskItem myTask = new TaskItem(); 

         myTask.Subject = mail.TaskSubject.ToString(); 
         myTask.ToDoTaskOrdinal = mail.ToDoTaskOrdinal; 
         if (mail.TaskCompletedDate.Equals(nonDate)) 
         { 
          myTask.Complete = false; 
         } 
         else 
         { 
          myTask.Complete = true; 
         } 

         taskList.Add(myTask); 
        } 
       } 
      } 
     } 
    } 

    if (taskList.Count == 0) 
    { 
     body += "\tNo tasks found...\n"; 
    } 
    else 
    { 
     Boolean completeTasksFound = false; 

     // Sort the task list by the ordinals and then add to body of email 
     taskList.Sort(); 
     foreach (TaskItem task in taskList) 
     { 
      String taskLineOut = "\t" + task.Subject; 
      if (task.Complete) 
      { 
       taskLineOut += "*"; 
       completeTasksFound = true; 
      } 
      taskLineOut += "\n"; 

      body += taskLineOut; 
     } 

     if (completeTasksFound) 
     { 
      body += "* completed\n\n"; 
     } 
     else 
     { 
      body += "\n"; 
     } 
    } 

    if (append) 
    { 
     myMailItem.Body += body; 
    } 
    else 
    { 
     myMailItem.Body = body; 
    } 

    Cursor.Current = Cursors.Default; 
} 

是否有該文件夾中過濾項目的一些方法? Find和FindNext方法看起來很有前途,但是我找不到如何指定將使用的過濾器應用於項目的IsMarkedAsTask和TaskDueDate元素。

+0

這是怎麼題外話? – 2011-02-02 00:48:08

+0

我建議你停止使用`ArrayList`。爲什麼不用`List `代替? – 2011-02-02 00:48:54

回答

相關問題