2011-02-06 86 views
3

我正在写一个程序,检查预约的前景,基本上我只需要当前的任命 - 如果当前没有预约,然后就在旁边,甚至以前的预约。检索当前Outlook约会

我想这样做我需要使用[restrict method] [1]来限制约会集合,然后根据restrict参数选择第一个或最后一个约会(例如,仅限于约会结束在当前时间之后,或者仅在当前时间之前开始约会)。 我在作为参数需要的字符串过滤器方面遇到了很多麻烦。

一个简单的例子VB(代码残端)如下([源] [2])

myStart = Format(Date, "mm/dd/yyyy hh:mm AMPM")  
strRestriction = "[Start] <= '" & myStart & "'" 

'Restrict the Items collection 
Set oResItems = oItems.Restrict(strRestriction) 
'Sort 
oResItems.Sort "[Start]" 

但是,试图做同样在C#似乎并不奏效。

// Create the Outlook application. 
Outlook.Application oApp = new Outlook.Application(); 

// Get the NameSpace and Logon information. 
// Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi"); 
Outlook.NameSpace oNS = oApp.GetNamespace("mapi"); 

//Log on by using a dialog box to choose the profile. 
oNS.Logon(Missing.Value, Missing.Value, true, true); 

// Get the Calendar folder. 
Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); 

// Get the Items (Appointments) collection from the Calendar folder. 
oItems = oCalendar.Items; 
oItems.IncludeRecurrences = true; 

// THIS IS THE PROBLEM AREA 
String filter = "[Start] <= '" + DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM") + "'"; 
Outlook.Items restrictedItems = oItems.Restrict(filter); 
// Take the last item on the list - should be current or next appointment 
restrictedItems.Sort("[Start]"); 
Outlook.AppointmentItem oAppt = restrictedItems.GetLast(); 


// Done. Log off. 
oNS.Logoff(); 

首先,我想,因为所述过滤器是一个字符串,日期格式必须是YYYY/MM/DD hh:mm:ss的?我似乎无法找到如何操作[开始],喜欢它解析为一个日期或东西的任何文档(我尝试了各种各样的事情,但没有任何工程)。根据我使用的日期格式,我将在得到错误的约会,否则程序将无法使用GetLast由于不包括所有约会的过滤器。我已经看到几个人声称他们已经开始工作的例子,但他们要么循环约会(效率太低),要么日期格式看起来像他们不能被信任返回正确的任命(好像是这个家伙social.msdn.microsoft.com/Forums/en-US/vsto/thread/c6a8bd21-6534-43be-b23e-1068651da92e,谁也似乎如果使用已经硬编码的日期,而不是DateTime.Now ..)

更新:我决定我不想浪费更多时间来尝试工作的限制角度,所以我目前只是循环如下所示,但任何建议更有效的代码非常欢迎。

DateTime currentTime = DateTime.Now; 
foreach (Outlook.AppointmentItem item in oItems) 
{ 
    if (item.Start <= currentTime && item.End.Subtract(new TimeSpan(0, 10, 0)) > currentTime) 
    { 
     appointmentArrayList.Add(item); 
    } 
} 

回答

2

通过以下信息发现here,我能得到它来工作“YYYY-MM-DD HH:MM”的格式字符串了toString电话。

希望这会有所帮助。

+0

非常感谢!一切似乎都像现在一样工作。似乎我忘记了M的月份:S – Madsn 2011-02-19 23:54:16

3

这是你的问题:

DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM") 

我想你会的是:

DateTime.Now.ToString("MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture) 
0

此代码的工作,显示从今天起Outlook约会:

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     DemoAppointmentsInRange(); 
    } 

    private void DemoAppointmentsInRange() 
    { 
     Application a = new Application(); 
     Microsoft.Office.Interop.Outlook.Folder calFolder = a.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar) as Microsoft.Office.Interop.Outlook.Folder; 
     DateTime start = DateTime.Now; 
     DateTime end = start.AddDays(5); 
     Microsoft.Office.Interop.Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end); 
     if (rangeAppts != null) 
     { 
      foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts) 
      { 
       Response.Write("Subject: " + appt.Subject + " "+" Start: "+appt.Start.ToString()+" "+"End:"+appt.End.ToString()+"<br/>"); 
      } 
     } 
    } 
    private Microsoft.Office.Interop.Outlook.Items GetAppointmentsInRange(
    Microsoft.Office.Interop.Outlook.Folder folder, DateTime startTime, DateTime endTime) 
    { 
     string filter = "[Start] >= '"+ startTime.ToString("g")+ "' AND [End] <= '" + endTime.ToString("g") + "'"; 
     //Response.Write(filter); 
     try 
     { 
      Microsoft.Office.Interop.Outlook.Items calItems = folder.Items; 
      calItems.IncludeRecurrences = true; 
      calItems.Sort("[Start]", Type.Missing); 
      Microsoft.Office.Interop.Outlook.Items restrictItems = calItems.Restrict(filter); 
      if (restrictItems.Count > 0) 
      { 
       return restrictItems; 
      } 
      else 
      { 
       return null; 
      } 
     } 
     catch 
     { 
      return null; 
     } 
    } 
} 
+0

请解释您的代码的作用。 – 2017-12-01 09:53:38