[英]C# Outlook VSTO add-in error while trying to get item in an Exchange setup
我正在用C#开发Office VSTO加载项,该加载项在Outlook中查找日历约会,并在其中读取/写入一些数据。
最近,其中一位客户的外接程序有问题,即他们无法读取/写入日历约会,并且抛出异常:
操作失败。
异常日志中没有太多信息,但是我怀疑它们在与Exchange同步方面存在问题。
我问客户,他们说,他们在Outlook中也有一个随机弹出窗口,有时在与Exchange同步时发生意外时会发生这种情况。 我告诉他们“修复” Outlook数据文件,但这并不能解决问题。
基本上基于Outlook EntryID或Subject查找Outlook项目(这些主题是唯一的,为了简单起见,我翻译了一下代码)
...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中更改“本地数据”,然后让Outlook处理所有内容)
您需要立即释放基础COM对象。 使用完后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。 如果您的加载项尝试枚举Microsoft Exchange Server上存储的集合中的256个以上Outlook项目,则这尤其重要。 如果不及时释放这些对象,则可以达到Exchange对任何一次打开的最大项目数施加的限制。 然后在Visual Basic中将变量设置为Nothing(在C#中为null)以释放对该对象的引用。 在“ 系统发布对象”文章中了解有关此内容的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.