我在C#中开发Office VSTO加载项,在Outlook中查找日历约会,并在其中读取/写入一些数据。
最近,其中一个客户端出现了加载项问题,即他们无法读取/写入日历约会,并引发异常:
操作失败。
异常日志中的信息不多,但我怀疑它们与Exchange存在同步问题。
我问客户,他们说,他们在Outlook中也有一个随机弹出窗口,有时会在与Exchange同步时发生意外。我告诉他们要修理' Outlook数据文件,但没有解决问题。
Outlook项目基本上是基于他们的Outlook EntryID s或者一个主题(主题是唯一的,为了简单起见,我翻译了一些代码)
...main alghorythm...
Outlook.AppointmentItem calAppointment = null;
calAppointment = SearchforCalendarMatch(EntryID, Subject); //we try to find either by EntryID or by Subject
if (calAppointment != null)
{
calAppointment.Start = StartDate;
calAppointment.End = FinishDate;
calAppointment.Body = Notes;
calAppointment.Save(); //we're changing the found calendar appointment here
}
...
public Outlook.AppointmentItem SearchforCalendarMatch(String EntryID, String Subject)
{
Outlook.NameSpace ns = null;
Outlook.MAPIFolder calendarFolder = null;
Outlook.Items calendarFolderItems = null;
Outlook.Items filteredcalendarFolderItems = null;
Outlook.AppointmentItem calAppointment = null;
Outlook.Application OutlookApp = new Outlook.Application();
outlookversion = OutlookApp.Version;
ns = OutlookApp.Session;
//Try to find the calendar appointment by the EntryID
dynamic OutlookItem = ns.GetItemFromID(t.Text28);
if (OutlookItem != null)
{
if (OutlookItem is Outlook.AppointmentItem)
{
Outlook.AppointmentItem foundItem = (Outlook.AppointmentItem)OutlookItem;
return foundItem;
}
}
//If the EntryID was missing, we try to find the calendar appointment by the Subject.
//(original code is very long, and there are multiple things here, but let's just assume that 100% sure that the subject is unique, so it will find it)
String SubjectMatch = "[Subject] = '" + Subject + "'";
filteredcalendarFolderItems = calendarFolderItems.Restrict(SubjectMatch);
for (int i = 1; i <= filteredcalendarFolderItems.Count; i++)
{
//appointment has to be one of these
calAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)filteredcalendarFolderItems[i];
if (!calAppointment.IsConflict) //conflict check here, not sure if it helps at all
{
return calAppointment; //this is not the complete code, but this is the basic idea of it.
}
}
}
我是如何让应用程序识别这些失败的Exchange同步并以不同方式处理它们的想法?
我仍然希望在这些情况下同步,如果可能的话......(更改Outlook中的本地数据&#39;然后让Outlook处理所有内容)
答案 0 :(得分:0)
您需要立即释放基础COM对象。完成使用后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。如果您的加载项尝试枚举存储在Microsoft Exchange Server上的集合中超过256个Outlook项目,这一点尤为重要。如果您未及时发布这些对象,则可以达到Exchange对任何时候打开的最大项目数的限制。然后在Visual Basic中将变量设置为Nothing(C#中为null)以释放对该对象的引用。请在Systematically Releasing Objects文章中详细了解相关内容。