是否可以重新加载 outlook Office.ComAddIn 列表?

Is it possible to reload outlook Office.ComAddIn list?

背景是我有一个旧插件,我用它来安装其他插件并删除 ThisAddIn_Startup 上的旧插件。一切似乎都很好,但发现有一台计算机的解决方案不起作用。

我成功安装和删除插件,但第一次加载时插件不加载。

private static void EnableNewPlugin()
{
    Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
    object index = "NewAddin";
    Office.COMAddIn addin = null;
    addin = app.COMAddIns.Item(ref index);
    addin.Connect = false;
    addin.Connect = true;
}

所以这个方法适用于所有其他计算机,但问题出在这台电脑上,它似乎没有在 app.COMAddIns 列表中找到它。所以我的问题是如何以编程方式重新启动 COMAddIns 列表?

如果有人遇到同样的问题,这似乎对我有用。

COMAddIns.Update();

COMAddins 集合的 Update 方法用于刷新 Windows 注册表中的 COM 加载项列表。

但是为什么需要在代码中新建一个Outlook应用实例呢?您是否开发 Outlook 加载项?如果是这样,您可以使用加载项 class 的应用程序 属性。或者您可以在运行时使用 Marshal class 的 GetActiveObject 方法获取 运行 Outlook 实例的实例。有关详细信息,请参阅 How to: Get and Log On to an Instance of Outlook

我还建议打破调用链并在单独的代码行上声明每个 属性 或方法调用。因此,您将能够立即释放所有底层 COM 对象。使用 MSDN 中的 System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. 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 文章。