以编程方式编辑约会

时间:2016-05-27 15:05:41

标签: c# outlook

我正在使用Microsoft.Office.Interop.Outlook将约会加载到界面中并编辑约会。我可以完全加载约会,编辑确实有效但不是100%的时间。有时我会收到错误The operation cannot be performed because the message has been changed

请问有没有更好的方法来编辑约会以避免The operation cannot be performed because the message has been changed错误。尝试Save()

时出错
public Outlook.AppointmentItem EditOutlookAppointment(CustomAppointment appointment, int retries = 0)
{
    Outlook.AppointmentItem appointReturned = null;
    try
    {
        Outlook.Application outlookApp = new Outlook.Application();
        MAPIFolder calendarFolder = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

        for (int i = calendarFolder.Items.Count; i > 0; i--)
        {
            var appointmentItem = ((Outlook.AppointmentItem)calendarFolder.Items[i]);

            if (appointmentItem.GlobalAppointmentID == appointment.UniqueId)
            {
                // set some properties
                appointmentItem.Subject    = appointment.Subject;
                appointmentItem.Body       = appointment.Body;
                appointmentItem.Location   = appointment.Location;            //Set the location
                appointmentItem.Start = appointment.Start;
                appointmentItem.End   = appointment.End; 
                appointmentItem.Save();
                return appointmentItem;
            }
        }
    }
    catch (Exception ex)
    {
            //Error message implementation here
    }
    return appointReturned;
}

1 个答案:

答案 0 :(得分:1)

首先,这样的错误是不可避免的 - 这意味着在Outlook打开它和调用Save之间修改约会。而且由于Outlook真的喜欢缓存约会(它总是缓存在Outlook中编辑的约会或之前编辑的约会),那段时间可能非常大。如果Exchange服务器本身或传入的会议更新处理修改了约会,则会发生这种情况。

其次,循环遍历Calendar文件夹中的所有项目可能是一个巨大的性能问题。不幸的是,Outlook不允许您在二进制(PT_BINARY)属性(例如Items.Find/FindNext)上搜索(Items.RestrictGlobalAppointmentID)。您需要使用扩展MAPI(C ++或Delphi)或Redemption(任何语言)(Redemption中的RDOtems .Find/FindNext/Restrict允许搜索二进制属性)。

更新。以下应该使用Redemption(在我的头顶):

publicRedemption.RDOAppointmentItem EditOutlookAppointment(CustomAppointment appointment, int retries = 0)
{
    try
    {
        Outlook.Application outlookApp = new Outlook.Application();
        Redemption.RDOSession session = new Redemption.RDOSession();
        session.MAPIOBJECT = outlookApp.Session.MAPIOBJECT; //share the Outlook session
        RDOFolder calendarFolder = session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
        Redemption.RDOAppointmentItem appointmentItem  = calendarFolder.Items.Find("GlobalAppointmentID = '"+appointment.UniqueId + "'");

        if (appointmentItem != null)
        {
                // set some properties
                appointmentItem.Subject    = appointment.Subject;
                appointmentItem.Body       = appointment.Body;
                appointmentItem.Location   = appointment.Location;            //Set the location
                appointmentItem.Start = appointment.Start;
                appointmentItem.End   = appointment.End; 
                appointmentItem.Save();
                return appointmentItem;
        }
    }
    catch (Exception ex)
    {
            //Error message implementation here
    }
    return null;
}