我已经使用可扩展性IDTEXTENSIBILIS2接口在Visual Studio 2008中为Excel开发了共享的Addin。

它在功能上是非常基本的。打开工作簿时,它将存储在打开的工作簿列表中,当它关闭时,Addin将创建一个新的文本文件,然后将一些XML写入文件,然后通过另一个进程读取该文件,然后将其读取,然后将XML删除。

ADDIN在正常操作下工作 - 因此,如果用户打开并关闭文件,则Addin可以按照自己的意愿进行操作,如果他们用开放的工作簿退出Excel,那么它应该做到的。

问题是用户在开放式工作簿中脱颖而出并进行注销时。这两种方法:ondisconnection和OnBeginShutdown似乎根本没有被调用。

我做了两件事来测试这一点:

  1. 我创建了一个TextWriterTracelistener,该词汇将在调用这两种方法时写入日志文件。当Excel被戒烟时,它们会被击中并在日志文件中记录信息,但是当用户注销时,日志文件中没有任何内容。

  2. 在这两种方法中都使用file.createText(文件名)我创建了一个空白文件。当用户正常辞职时,创建了这些文件,但是当通过注销关闭Excel时,这些文件并未创建这些文件。

有人有任何想法如何解决这个问题吗?当用户登录其机器时,我需要在关闭Excel时捕获...

有帮助吗?

解决方案

最终的解决方案是将 Microsoft.Win32.SystemEvents.SessionEnding 而且,当发射此系统事件时,请手动致电 OnBeginShutdown 方法。

其他提示

这用于导致对象〜在VB6天内失败误差的方法。

尝试WorkbookBeforeClose或潜在的保护Windowbeforeclose。

如果我没记错的话,您可能会遇到的一个问题,即您无法捕获何时取消事件激活或开放的事件,以便如果用户取消关闭操作,则您的addin将是可用的。

希望这是有道理的。

我的Outlook 2010 Addin也有同样的问题。这可能与以下事实有关 Outlook 2010 才不是 信号加载程序正在关闭.

具体来说,Outlook [2010]不再致电 OnBeginShutdown开发 快速关闭期间的IDTEXTENSENIBLE2接口的方法。

同样,使用Microsoft Visual Studio工具编写的Outlook加载项不再致电 thisaddin_shutdown 当Outlook关闭时的方法。

如果您仍然希望在Outlook 2010关闭时通知您的插件(就像我一样),则需要锁定 Application' ApplicationEvents_Event_Quit 事件,使用下面的我的代码(关闭代码仍应在两个中都运行 OnDisconnectionOnBeginShutdown 无论如何):

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
    // As this is an Outlook-only extension, we know the application object will be an Outlook application
    _applicationObject = (Microsoft.Office.Interop.Outlook.Application)application;

    // Make sure we're notified when Outlook 2010 is shutting down
    ((Microsoft.Office.Interop.Outlook.ApplicationClass)_applicationObject).ApplicationEvents_Event_Quit += new ApplicationEvents_QuitEventHandler(Connect_ApplicationEvents_Event_Quit);
}

private void Connect_ApplicationEvents_Event_Quit()
{
    Array emptyCustomArray = new object[] { };
    OnBeginShutdown(ref emptyCustomArray);
}

public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
    addinShutdown();
}

public void OnBeginShutdown(ref System.Array custom)
{
    addinShutdown();
}

private void addinShutdown()
{
    // Code to run when addin is being unloaded, or Outlook is shutting down, goes here...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top