2012-02-22 143 views
0

,當我寫了這個代碼,我有兩個問題是:慢的代碼連接到Outlook日曆

  1. 它太慢
  2. 它僅顯示在日曆中定期約會。我希望它不僅顯示來自特定日期的定期約會,還顯示該日期以及從指定日期開始的所有其他約會。

幫助將不勝感激。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Reflection; 
using Outlook = Microsoft.Office.Interop.Outlook; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public void GetAllCalendarItems() 
     { 
      try 
      { 
       // outlook application object 
       Outlook.Application oApp = new Outlook.Application(); 
       //folder in outlook 
       Outlook.Folders OutlookFolders = oApp.Session.Folders; 

       //Mapi folder object 
       Outlook.MAPIFolder Folder = null; 
       // outlook exploer object 
       Outlook.Explorer explorer = null; 
       //Map subfolder object 
       Outlook.MAPIFolder SubFolder = null; 

       // folder int control 
       int Folderctr; 
       // sub folder control 
       int subFolderctr;     

       // initialize base objects 

       //date range to get appointments 
       DateTime start = DateTime.Now; 
       DateTime end = start.AddDays(5); 

       // loop throught the PST files added on Outlook 
       for (Folderctr = 1; Folderctr <= OutlookFolders.Count; Folderctr++) 
       { 
        Folder = OutlookFolders[Folderctr]; 
        Console.WriteLine("first name of folder " +Folder.Name); 
        explorer = Folder.GetExplorer(); 
        //loop throught the folders in the PST files 
        for (subFolderctr = 1; subFolderctr <= explorer.CurrentFolder.Folders.Count; subFolderctr++) 
        { 
         SubFolder = explorer.CurrentFolder.Folders[subFolderctr]; 

         if (SubFolder.Name == "SCE_Calendar") 
         { 
          GetAllCalendarItems(SubFolder);  

          Console.WriteLine("subfolder name of folder " + SubFolder.Name); 
         } 
        } 
       } 

       //close applciation 
       oApp.Quit(); 
       // release com object 
       System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp); 
       oApp = null; 
      }  
      //Simple error handling. 
      catch (Exception e) 
      { 
       Console.WriteLine("{0} Exception caught.", e); 
      } 
     } 

     public void GetAllCalendarItems(Outlook.MAPIFolder seCal) 
     { 
      Outlook.Items outlookCalendarItems = null; 
      //set recursion to true 
      outlookCalendarItems = seCal.Items; 
      outlookCalendarItems.IncludeRecurrences = true; 
      DateTime last; 
      DateTime first; 

      foreach (Outlook.AppointmentItem item in outlookCalendarItems) 
      { 
       Outlook.RecurrencePattern rp = item.GetRecurrencePattern(); 
       first = new DateTime(2012, 2, 1, item.Start.Hour, item.Start.Minute, 0); 
       last = new DateTime(2012, 2, 29); 
       Outlook.AppointmentItem recur = null; 

       for (DateTime cur = first; cur <= last; cur = cur.AddDays(1)) 
       { 
        try 
        { 
         recur = rp.GetOccurrence(cur); 
         //Console.WriteLine(recur.Subject + " -> " + cur.ToLongDateString()); 
         Console.WriteLine("Subject: " + recur.Body); 
         Console.WriteLine("Organizer: " + recur.Organizer); 
         Console.WriteLine("Start: " + recur.Start.ToString()); 
         Console.WriteLine("End: " + recur.End.ToString()); 
         Console.WriteLine("Location: " + recur.Location); 
         Console.WriteLine("Recurring: " + recur.IsRecurring); 
         Console.WriteLine("-----------------------------------------"); 
         Console.WriteLine(" "); 
         //Show the item to pause. 
        } 
        catch 
        { } 
       } 
      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      GetAllCalendarItems(); 
     } 
    } 
} 
+1

發佈您的所有代碼並尋求幫助改進它並不是一個真正的建設性問題。我會建議提煉您的問題,以顯示導致您遇到問題的代碼部分,並提供您遇到的問題的詳細說明。 – 2012-02-22 15:21:09

回答

1

這是我用來獲取Outlook約會的代碼。我已經把它從一個工作程序中拉出來了,所以我可能會缺少一個輔助函數,但是這會幫助你開始。

private List<Outlook.AppointmentItem> GetOutlookAppointments(DateTime start, DateTime end) { 
    var outlook = new Microsoft.Office.Interop.Outlook.Application(); 
    var app = outlook.Application; 
    var appSession = app.Session; 
    var calendar = appSession.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); 
    string filter = String.Format("[Start] >= {0} And [End] < {1}", start.ToString("ddddd h:nn AMPM"), end.ToString("ddddd h:nn AMPM")); 
    var items = calendar.Items; 
    items.Restrict(filter); 
    items.IncludeRecurrences = true; 

    Outlook.AppointmentItem appointment; 
    var appointments = new List<Outlook.AppointmentItem>(); 
    foreach (var item in items) 
    { 
     appointment = item as Outlook.AppointmentItem; 
     if (appointment != null && !appointment.AllDayEvent && (appointment.BusyStatus != Outlook.OlBusyStatus.olOutOfOffice)) 
     { 
      if (appointment.IsRecurring) 
      { 
       AddReoccuringAppointments(appointments, start, end, appointment); 
      } 
      else if (appointment.Start >= start && appointment.End < end) 
      { 
       appointments.Add(appointment); 
      } 
     } 
    } 
} 

private static void AddReoccuringAppointments(List<Outlook.AppointmentItem> appointments, DateTime start, DateTime end, Outlook.AppointmentItem appointment) 
{ 
    Outlook.RecurrencePattern rp = appointment.GetRecurrencePattern(); 
    if (rp.PatternStartDate >= end || rp.PatternEndDate <= start) { return; } 
    if (rp.PatternStartDate > start) 
    { 
     start = rp.PatternStartDate; 
    } 
    if (rp.PatternEndDate < end) 
    { 
     end = rp.PatternEndDate; 
    } 

    var exceptions = GetExceptions(rp, start, end); 

    Outlook.AppointmentItem recur = null; 
    Outlook.Exception exception = null; 
    for (DateTime cur = new DateTime(start.Year, start.Month, start.Day, appointment.Start.Hour, appointment.Start.Minute, 0); cur <= end; cur = cur.AddDays(1)) 
    { 
     if ((((int)rp.DayOfWeekMask) & (int)Math.Pow(2,(int)cur.DayOfWeek)) == 0) { continue; } 
     exception = exceptions.FirstOrDefault(e => e.OriginalDate.Date == cur.Date); 
     if (exception == null) 
     { 
      recur = rp.GetOccurrence(cur); 
     } 
     else if (!exception.Deleted) 
     { 
      recur = exception.AppointmentItem; 
     } 

     if (recur != null) 
     { 
      appointments.Add(recur); 
     } 
    } 
} 

private static List<Outlook.Exception> GetExceptions(Outlook.RecurrencePattern rp, DateTime start, DateTime end) 
{ 
    List<Outlook.Exception> exceptions = new List<Outlook.Exception>(); 
    foreach (Outlook.Exception e in rp.Exceptions) 
    { 
     if (e.OriginalDate >= start && e.OriginalDate < end) 
     { 
      exceptions.Add(e); 
     } 
    } 
    return exceptions; 
}