【问题标题】:C# Outlook VSTO add-in error while trying to get item in an Exchange setup尝试在 Exchange 设置中获取项目时出现 C# Outlook VSTO 加载项错误
【发布时间】:2017-05-04 12:21:43
【问题描述】:

我正在用 C# 开发一个 Office VSTO 加载项,它在 Outlook 中查找日历约会,并在其中读取/写入一些数据。

最近,其中一位客户遇到了插件问题,即他们无法读取/写入日历约会,并引发异常:

操作失败。

异常日志中没有太多信息,但我怀疑它们与 Exchange 存在同步问题。

我问过客户,他们说,他们在 Outlook 中也有一个随机弹出窗口,这有时会在与 Exchange 同步时发生意外时发生。我告诉他们“修复” Outlook 数据文件,但这并没有解决问题。

outlook 项目基本上是根据其 Outlook EntryIDs OR a Subject 来查找的(主题是唯一的,为了简单起见,我稍微翻译了代码)

...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 处理此后的所有内容)

【问题讨论】:

    标签: c# outlook vsto outlook-addin office-addins


    【解决方案1】:

    您需要立即释放底层 COM 对象。使用完后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject 释放 Outlook 对象。如果您的加载项尝试枚举存储在 Microsoft Exchange Server 上的集合中的 256 个以上的 Outlook 项目,这一点尤其重要。如果您不及时释放这些对象,您可能会达到 Exchange 对任何一次打开的最大项目数量的限制。然后在 Visual Basic 中将变量设置为 Nothing(在 C# 中为 null)以释放对对象的引用。在Systematically Releasing Objects 文章中阅读更多相关信息。

    【讨论】:

    • 嗨尤金!在上面的代码中,我应该在哪里实现 ReleaseComObject?另外,它基本上只对底部有效,我实际上在哪里浏览集合?我应该“释放”所有 Excel 对象吗?我要退货的那个呢?
    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多