2013-01-10 86 views
2

我想使用C#按日期列出(提取)Outlook約會。我用限制的方法來做到這一點,使用C#在Outlook中按日期列出約會

sSearch = "[Start] >= ' " + startDate + " ' and [Start] <= ' " + endDate + " '"; 

但是,如果結束日期後的第二天(結束日期)具有全日任命它也列出。如何克服這個問題?

+0

yappa,請參閱下面的答案。這會讓你去,也請訪問鏈接 –

回答

3

http://msdn.microsoft.com/en-us/library/office/gg619398(v=office.14).aspx

private void DemoAppointmentsInRange() 
{ 
    Outlook.Folder calFolder = 
     Application.Session.GetDefaultFolder(
     Outlook.OlDefaultFolders.olFolderCalendar) 
     as Outlook.Folder; 
    DateTime start = DateTime.Now; 
    DateTime end = start.AddDays(5); 
    Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end); 
    if (rangeAppts != null) 
    { 
     foreach (Outlook.AppointmentItem appt in rangeAppts) 
     { 
      Debug.WriteLine("Subject: " + appt.Subject 
       + " Start: " + appt.Start.ToString("g")); 
     } 
    } 
} 

/// <summary> 
/// Get recurring appointments in date range. 
/// </summary> 
/// <param name="folder"></param> 
/// <param name="startTime"></param> 
/// <param name="endTime"></param> 
/// <returns>Outlook.Items</returns> 
private Outlook.Items GetAppointmentsInRange(
    Outlook.Folder folder, DateTime startTime, DateTime endTime) 
{ 
    string filter = "[Start] >= '" 
     + startTime.ToString("g") 
     + "' AND [End] <= '" 
     + endTime.ToString("g") + "'"; 
    Debug.WriteLine(filter); 
    try 
    { 
     Outlook.Items calItems = folder.Items; 
     calItems.IncludeRecurrences = true; 
     calItems.Sort("[Start]", Type.Missing); 
     Outlook.Items restrictItems = calItems.Restrict(filter); 
     if (restrictItems.Count > 0) 
     { 
      return restrictItems; 
     } 
     else 
     { 
      return null; 
     } 
    } 
    catch { return null; } 
} 
+0

玩結束日期時間戳,這可能導致問題 –

+0

string filter =「[開始] <='」 + endTime.ToString(「g」) +「 'AND [End]> ='' + startTime.ToString(「g」)+「'」; –

3

到潭潭的類似。少一點代碼。我花了整整一天的時間尋找我可以在LinqPad中使用的基本樣本。這是我結束了。

//using Microsoft.Office.Interop.Outlook 
Application a = new Application(); 
Items i = a.Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar).Items; 
i.IncludeRecurrences = true; 
i.Sort("[Start]"); 
i = i.Restrict("[Start] >= '10/1/2013' AND [End] <= '10/2/2013'"); 
var r = 
    from ai in i.Cast<AppointmentItem>() 
    select new {ai.Start,ai.Duration,ai.Subject}; 
r.Dump(); 
+0

不錯!我愛linqpad! –