尝试在Exchange安装程序中获取项目时出现C#Outlook VSTO加载项错误 [英] C# Outlook VSTO add-in error while trying to get item in an Exchange setup
问题描述
我正在用C#开发Office VSTO加载项,该加载项在Outlook中查找日历约会,并在其中读取/写入一些数据.
I'm developing an Office VSTO add-in in C# which looks up calendar appointments in Outlook, and reads/writes some data in them.
最近,其中一位客户的外接程序有问题,即他们无法读/写日历约会,并且抛出异常:
Recently, one of the clients had issues with the add-in, namely they can't read/write the calendar appointment, and it throws an exception :
操作失败.
The operation failed.
异常日志中没有太多信息,但我怀疑它们与Exchange存在同步问题.
There's not much info from the exception log, but I suspect they have synchronization problems with Exchange.
我问客户,他们说,他们在Outlook中也有一个随机弹出窗口,有时在与Exchange同步时发生意外时会发生这种情况.我告诉他们修复" Outlook数据文件,但这并不能解决问题.
I asked the client, and they said, that they also have a random popup in Outlook as well, which sometimes happens when there's a mishap while syncing with Exchange. I told them to 'repair' the Outlook data files, but that didn't fix the problem.
基本上根据Outlook的Outlook EntryID 或主题(主题是唯一的,为了简单起见,我翻译了一些代码)来查找Outlook项目
The outlook items are basically looked up based on their Outlook EntryIDs OR a Subject (the subjects are unique, and for the sake of simplicity I translated the code a little bit)
...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同步,并以不同的方式处理它们?
Any ideas how I could make the application recognize these failed Exchange syncs, and handle them differently?
如果可能的话,我仍然想在这些情况下进行同步...(在Outlook中更改本地数据",然后让Outlook处理所有内容)
I would still like to sync in these cases, if it's possible... (change the 'local data' in Outlook, then let Outlook handle everything from that on)
推荐答案
您需要立即释放基础COM对象.使用 System.Runtime.InteropServices.Marshal.ReleaseComObject 以在使用完Outlook对象后将其释放.如果您的加载项尝试枚举Microsoft Exchange Server上存储的集合中的256个以上Outlook项目,则这一点尤其重要.如果不及时释放这些对象,则可以达到Exchange对任意一次打开的最大项目数施加的限制.然后在Visual Basic中将变量设置为Nothing(在C#中为null)以释放对该对象的引用.在系统地发布对象文章.
You need to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.
这篇关于尝试在Exchange安装程序中获取项目时出现C#Outlook VSTO加载项错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!