我想從任何給定用戶的名字檢索當天的約會。 到目前爲止,我可以通過下面的代碼檢索我自己的約會。檢索公共Outlook日曆約會?
else if (UserSelection == "2")
{
//Create the Outlook application
Outlook.Application oApplication = new Outlook.Application();
// Get the NameSpace and Logon information.
Outlook.NameSpace oNameSpace = oApplication.GetNamespace("mapi");
//Log on by using a dialog box to choose the profile.
oNameSpace.Logon(Missing.Value, Missing.Value, true, true);
// Get the Calendar folder.
Outlook.MAPIFolder oCalendar = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
// Get the appointments (items) collection from the Calendar folder.
Outlook.Items oItems = oCalendar.Items;
oItems.IncludeRecurrences = true;
List<Outlook.AppointmentItem> lst = new List<Outlook.AppointmentItem>();
foreach (Outlook.AppointmentItem item in oItems)
{
if (item.Start.Day == DateTime.Today.Day && item.Start.Month == DateTime.Today.Month && item.Start.Year == DateTime.Today.Year)
{
Console.WriteLine("Organizer: " + item.Organizer);
Console.WriteLine("Start: " + item.Start.ToString());
Console.WriteLine("End: " + item.End.ToString());
Console.WriteLine("Location: " + item.Location);
Console.WriteLine("Recurring: " + item.IsRecurring);
Console.WriteLine("Subject: " + item.Subject);
Console.WriteLine("Attendees: " + item.OptionalAttendees);
Console.WriteLine("");
}
}
//Get the last appointment(item)
//Outlook.AppointmentItem oAppt = (Outlook.AppointmentItem)oItems.GetLast();
//Show the appointment(item) in outlook.
//oAppt.Display(true);
// Done. Log off.
oNameSpace.Logoff();
//Clean up.
oItems = null;
oCalendar = null;
oNameSpace = null;
oApplication = null;
Console.ReadLine();
這工作正常。但是,下面的代碼不會返回給定的用戶「userName」的約會。
if (UserSelection == "1")
//try (used for error handling)
{
string userName = Console.ReadLine();
Outlook.Application oApplication;
oApplication = new Outlook.Application();
Outlook.NameSpace oNameSpace = oApplication.GetNamespace("mapi");
oNameSpace.Logon(Missing.Value, Missing.Value, true, true);
Outlook.Recipient oRecip = (Outlook.Recipient)oNameSpace.CreateRecipient(userName);
Outlook.MAPIFolder oCalendar = (Outlook.MAPIFolder)oNameSpace.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar);
// Get the appointments (items) collection from the Calendar folder.
Outlook.Items oItems = oCalendar.Items;
oItems.IncludeRecurrences = true;
List<Outlook.AppointmentItem> lst = new List<Outlook.AppointmentItem>();
foreach (Outlook.AppointmentItem item in oItems)
{
if (item.Start.Day == DateTime.Today.Day && item.Start.Month == DateTime.Today.Month && item.Start.Year == DateTime.Today.Year)
{
Console.WriteLine("Organizer: " + item.Organizer);
Console.WriteLine("Start: " + item.Start.ToString());
Console.WriteLine("End: " + item.End.ToString());
Console.WriteLine("Location: " + item.Location);
Console.WriteLine("Recurring: " + item.IsRecurring);
Console.WriteLine("Subject: " + item.Subject);
Console.WriteLine("Attendees: " + item.OptionalAttendees);
Console.WriteLine("");
}
}
//Show the appointment(item) in outlook.
//oAppt.Display(true);
// Done. Log off.
oNameSpace.Logoff();
// Clean up.
oItems = null;
oCalendar = null;
oNameSpace = null;
oApplication = null;
Console.ReadLine();
產生錯誤的代碼行是foreach (Outlook.AppointmentItem item in oItems)
返回的錯誤是「‘System.Runtime.InteropServices.COMException’類型的未處理的異常出現在mscorlib.dll
其他信息:從HRESULT異常:0x8834010F「
任何幫助,使這項工作將不勝感激,謝謝。
立即是否異常火災或只有你處理了幾個約會後? – 2015-03-31 14:38:09
只有輸入用戶名並按回車鍵後才輸入 – CheesyMeat 2015-03-31 14:45:17
如果我輸入自己的名字,則返回約會,但如果我輸入其他人,則返回錯誤。可能是權限問題?我有權訪問公共日曆,因此應該可以將約會拉好? – CheesyMeat 2015-03-31 16:00:57