VSTO Outlook插件:用户拖放定期约会时,无法在Item_Change事件中获取AppointmentItem [英] VSTO Outlook Plugin: Cannot get AppointmentItem in Item_Change event when recurring appointment is dragged and dropped by user
问题描述
我想在AppointmentItem上捕获更改事件.我使用Outlook 2017进行测试. 要实现我使用:
I want to catch the change event on an AppointmentItem. I use Outlook 2017 for tests. To achieve I use:
我附上了这样的事件:
public void AttachEvents()
{
_CalendarItems.ItemAdd += Item_Add;
_CalendarItems.ItemChange += Item_Change;
_DeletedItems.ItemAdd += Item_Delete_Add;
Item_Change方法如下:
The Item_Change method looks like this:
public void Item_Change(Object item)
{
if (item != null && item is Outlook.AppointmentItem)
{
Outlook.AppointmentItem myAppointment = item as Outlook.AppointmentItem;
为了测试代码,我创建了一个定期约会系列.我双击日历中的约会,然后输入一些标题和正文并保存. 现在,我开始编写代码并检查了该项目. 不幸的是,启动项目更改时,项目指向系列而不是单个约会. 启动Item_Changed时如何检索实际的AppointmentItem?
To test the code I created a recurring appointment series. I double-clicked on appointment in the calendar and entered some title and body and saved. Now I started my code and inspected the item. Unfortunately, item points to the series and NOT to the individual appointment when item changed is initiated. How can I retrieve the actual AppointmentItem when Item_Changed is initiated?
与Stackoverflow相关的帖子: Outlook插件:在日历中移动约会不会在AppointmentItem(捕获Calendar.ItemChange)中反映新的日期/时间,但是对此仍然没有解决办法
Related Stackoverflow Posting: Outlook Addin: Moving Appointment in Calendar does not reflect new date/time in AppointmentItem (catch Calendar.ItemChange) But still there is no solution to this
有关此主题的更多信息:
- https://www.add-in-express.com/forum/read.php?FID=5&TID=15384
- https://social.msdn.microsoft.com/Forums/sqlserver/en-US/4ec55891-fb64-408f-b1cf-4bf05765b866/outlook-get-original-time-of-recurring-exception-item-that-is-opened-with-drag-drop?forum=vsto
推荐答案
有一个棘手的解决方案. 假设:用户使用日历视图更改项目.由于日历视图会抛出该事件,因此在所有情况下都应为真:
There is a tricky solution to that. Assumption: The user uses the calendar view to change the item. As the event is thrown by the calendar view this should be true in all cases:
_CalendarItems = calendarFolder.Items;
_CalendarItems.ItemChange += Item_Change;
[...] 现在我们可以使用CalendarView计算选定的开始日期,并将其与RecurrencePattern中存储的所有Exception进行比较...
[...] now we can use the CalendarView to calculate the selected Startdate and compare it to all Exceptions stored in the RecurrencePattern ...
if (myAppointment.IsRecurring)
{
// in case of recurring appointments at this point we always get
// only a reference to the series master NOT the occurrence
// Assumption: The user clicked on the AppointmentItem in the calendar view
// So we can calculate the selected Start Time from this selection range
// then compare this against all Exceptions in the OccurrencePattern of the recurring pattern
// if we find one AppointmentItem in the Exceptions which has the same DateTime then we found the correct one.
//
Outlook.Application application = new Outlook.Application();
Outlook.Explorer explorer = application.ActiveExplorer();
Outlook.Folder folder = explorer.CurrentFolder as Outlook.Folder;
Outlook.View view = explorer.CurrentView as Outlook.View;
// get the current calendar view
if (view.ViewType == Outlook.OlViewType.olCalendarView)
{
Outlook.CalendarView calView = view as Outlook.CalendarView;
Outlook.RecurrencePattern pattern = myAppointment.GetRecurrencePattern();
for (int i = 1; i <= pattern.Exceptions.Count; i++)
{
Outlook.Exception myException = pattern.Exceptions[i];
Outlook.AppointmentItem exceptionItem = myException.AppointmentItem;
DateTime itemDateStart = exceptionItem.Start;
if (itemDateStart == calView.SelectedStartTime)
{
updateMyPluginMeeting(exceptionItem);
return; // the use may only select on AppointmentItem so we can skip the rest
}
}
}
}
如果您知道更好的解决方案,请告诉我.
If you know any better solution to this let me know.
这篇关于VSTO Outlook插件:用户拖放定期约会时,无法在Item_Change事件中获取AppointmentItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!