如何在机器上打开 Outlook 时从 Outlook 互操作获取定期约会 运行 代码?
How to get recurring appointments from Outlook interop when Outlook is open on the machine running the code?
我浪费了很多时间试图弄清楚为什么我会收到异常“您更改了该项目的一个重复项,并且该实例不再存在。关闭所有打开的项目并重试。”当 运行 以下代码时:
if(appointmentItem.IsRecurring)
{
RecurrencePattern recurrencePattern = appointmentItem.GetRecurrencePattern();
DateTime first =
new DateTime(
start.Year,
start.Month,
start.Day,
appointmentItem.Start.Hour,
appointmentItem.Start.Minute,
0
);
DateTime last = end.AddDays(1).Date.AddSeconds(-1);
AppointmentItem recurringAppointment;
for (DateTime date = first; date <= last; date = date.AddDays(1))
{
try
{
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.recurrencepattern.getoccurrence?view=outlook-pia#microsoft-office-interop-outlook-recurrencepattern-getoccurrence(system-datetime)
// The GetOccurrence method generates an error if no appointment of that series exists on the specified date.
recurringAppointment = recurrencePattern.GetOccurrence(date);
dataRow =
CreateDataRowFromAppointmentItem(
recurringAppointment
);
appointments.Rows.Add(dataRow);
}
catch (System.Exception)
{
// Exception is thrown if no
continue;
}
}
}
我终于意识到,如果 Outlook 未在计算机上打开,代码 运行 就可以工作!
如果用户在 运行 使用此代码的同一台计算机上打开 Outlook,您应该如何获得定期约会?
注意:当异常发生时,它发生在对 recurrencePattern.GetRecurrence(data)
的调用中
我更新了我的代码以获取这样的应用程序,现在一切正常:
private Result<Application> GetOutlookApplication()
{
try
{
Application outlookApplication =
Marshal.GetActiveObject(
"Outlook.Application"
) as Application;
if (outlookApplication != null)
return new Result<Application>(
outlookApplication
);
}
catch
{
}
try
{
Application outlookApplication = new Application();
//
// Microsoft says this will automatically initialize the session
// for the default profile if it isn't already initialized and
// to avoid calling Logon.
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook._namespace.logon?view=outlook-pia
return new Result<Application>(outlookApplication);
}
catch (System.Exception ex)
{
return new Result<Application>()
.WithException(ex);
}
}
获取现有实例 运行(如果可用)就可以了。以前我每次都创建一个新的应用程序对象。
我浪费了很多时间试图弄清楚为什么我会收到异常“您更改了该项目的一个重复项,并且该实例不再存在。关闭所有打开的项目并重试。”当 运行 以下代码时:
if(appointmentItem.IsRecurring)
{
RecurrencePattern recurrencePattern = appointmentItem.GetRecurrencePattern();
DateTime first =
new DateTime(
start.Year,
start.Month,
start.Day,
appointmentItem.Start.Hour,
appointmentItem.Start.Minute,
0
);
DateTime last = end.AddDays(1).Date.AddSeconds(-1);
AppointmentItem recurringAppointment;
for (DateTime date = first; date <= last; date = date.AddDays(1))
{
try
{
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.recurrencepattern.getoccurrence?view=outlook-pia#microsoft-office-interop-outlook-recurrencepattern-getoccurrence(system-datetime)
// The GetOccurrence method generates an error if no appointment of that series exists on the specified date.
recurringAppointment = recurrencePattern.GetOccurrence(date);
dataRow =
CreateDataRowFromAppointmentItem(
recurringAppointment
);
appointments.Rows.Add(dataRow);
}
catch (System.Exception)
{
// Exception is thrown if no
continue;
}
}
}
我终于意识到,如果 Outlook 未在计算机上打开,代码 运行 就可以工作!
如果用户在 运行 使用此代码的同一台计算机上打开 Outlook,您应该如何获得定期约会?
注意:当异常发生时,它发生在对 recurrencePattern.GetRecurrence(data)
我更新了我的代码以获取这样的应用程序,现在一切正常:
private Result<Application> GetOutlookApplication()
{
try
{
Application outlookApplication =
Marshal.GetActiveObject(
"Outlook.Application"
) as Application;
if (outlookApplication != null)
return new Result<Application>(
outlookApplication
);
}
catch
{
}
try
{
Application outlookApplication = new Application();
//
// Microsoft says this will automatically initialize the session
// for the default profile if it isn't already initialized and
// to avoid calling Logon.
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook._namespace.logon?view=outlook-pia
return new Result<Application>(outlookApplication);
}
catch (System.Exception ex)
{
return new Result<Application>()
.WithException(ex);
}
}
获取现有实例 运行(如果可用)就可以了。以前我每次都创建一个新的应用程序对象。