我有一个C#Windows窗体应用程序,从Outlook获取我的所有约会,并填写我的约会列表框但我只想要今天的所有约会,我通过使用下面的代码成功地做到了这一点。但我错过了下面列表中的重复项目,所以我每天早上9点到10点都会举行IT会议,并且经常出现为什么我无法在约会列表框中看到这个?
private void button2_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Outlook.RecurrencePattern recurrencePattern;
Microsoft.Office.Interop.Outlook.NameSpace oNS;
Microsoft.Office.Interop.Outlook.MAPIFolder oCalendar;
Microsoft.Office.Interop.Outlook.Items oItems;
Microsoft.Office.Interop.Outlook.AppointmentItem oAppt;
Microsoft.Office.Interop.Outlook.Application OutlookApplication;
try
{
OutlookApplication = new Microsoft.Office.Interop.Outlook.Application();
oNS = OutlookApplication.GetNamespace("MAPI");
// Get the Calendar folder.
Outlook.Recipient oRecip = (Outlook.Recipient)oNS.CreateRecipient(username.Text);
oCalendar = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
oCalendar.Items.IncludeRecurrences = true;
DateTime today = DateTime.Today;
// Get the Items (Appointments) collection from the Calendar folder.
oItems = oCalendar.Items;
for (Int32 x = 1; x <= oItems.Count; x++)
{
//Need to change how we are getting the appointment
//Apparently Outlook will return non-appointments in the calendar feed
try
{
oAppt = (Microsoft.Office.Interop.Outlook.AppointmentItem)oItems[x];
string now = DateTime.Today.ToString("dd/MM/yyy");
string jusdate = oAppt.Start.ToString("dd/MM/yyyy");
if (jusdate == now)
{
Appointments.Items.Add(oAppt.Subject);
from.Items.Add(oAppt.Start);
to.Items.Add(oAppt.End);
}
}
catch (Exception)
{
continue;
}
if (oAppt.Body != null)
Console.WriteLine(" Calendar Body:" + oAppt.Body.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
到目前为止我尝试了什么:
private void CheckOccurrenceExample()
{
Microsoft.Office.Interop.Outlook.Application OutlookApplication;
OutlookApplication = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace oNS;
oNS = OutlookApplication.GetNamespace("MAPI");
Outlook.AppointmentItem appt = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar).Items.Find("[Subject]='Recurring Appointment DaysOfWeekMask Example'") as Outlook.AppointmentItem;
if (appt != null)
{
try
{
Outlook.RecurrencePattern pattern = appt.GetRecurrencePattern();
Outlook.AppointmentItem singleAppt = pattern.GetOccurrence(DateTime.Parse( "11/13/2014 11:00 AM"))as Outlook.AppointmentItem;
if (singleAppt != null)
{
Appointments.Items.Add("11/13/2014 11:00 AM occurrence found.");
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
我在11/13/2014上午11:00进行了硬编码,因为我当时设置了定期约会用于测试目的,如果应用程序稍后工作,我将用DateTime.Today
替换