Excel 插件 - 注销时未调用 OnDisconnection/OnBeginShutdown

发布于 2024-10-06 14:41:01 字数 680 浏览 9 评论 0原文

我在 Visual Studio 2008 中使用可扩展性 IDTExtensibility2 接口开发了一个 Excel 共享插件。

它的功能非常基本。当工作簿打开时,它存储在打开的工作簿列表中,当关闭时,插件将创建一个新的文本文件,然后将一些 XML 写入该文件,然后该文件由另一个进程读取,然后该进程将反序列化 XML 。

该插件在正常操作下工作 - 因此,如果用户打开并关闭文件,那么插件会执行它应该执行的操作,如果他们退出 Excel 并打开工作簿,那么它会执行它应该执行的操作。

问题是当用户打开 Excel 并打开工作簿并注销时。这两个方法:OnDisconnection 和 OnBeginShutdown 似乎根本没有被调用。

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

  1. 我创建了一个 TextWriterTraceListener,它在调用这两个方法时写入日志文件。当 Excel 正常退出时,它们会被命中,信息会记录在日志文件中,但是当用户注销时,日志文件中没有任何内容。

  2. 在这两种方法中,我使用 File.CreateText(filename) 创建了一个空白文件。当用户正常退出 Excel 时,会创建这些文件,但再次,当通过注销关闭 Excel 时,不会创建这些文件。

有谁知道我如何解决这个问题?我需要捕获当用户注销其计算机时 Excel 被关闭的时间...

I have developed a Shared Addin for Excel using Extensibility IDTExtensibility2 interface in Visual Studio 2008.

It's quite basic in functionality. When a workbook is opened it is stored in a list of opened workbooks, when it is closed the addin will create a new text file then write out some XML to the file, this file is then read by another process which will then deserialize the XML.

The addin works under normal operations - so if the user opens and closes a file then the addin does what it should, if they quit Excel with open workbooks then it does what it should.

The problem is when the user has Excel open with open workbooks and does a Log-Off. The two methods: OnDisconnection and OnBeginShutdown don't appear to be called, at all.

I did two things to test this:

  1. I created a TextWriterTraceListener which wrote to a log file when these two methods were called. When Excel is quit normally they are hit and information is logged in the log file, but when a user logs off there is nothing in the log file.

  2. Inside both of these methods using File.CreateText(filename) I created a blank file. When the user quits Excel normally these files are created, but once again, when Excel is closed through a Log-Off these files aren't created.

Does anyone have any ideas how I can get around this problem? I need to capture when Excel is being closed when the user is logging off their machine...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

压抑⊿情绪 2024-10-13 14:41:01

最终的解决方案是挂钩 Microsoft.Win32.SystemEvents.SessionEnding,并在触发此系统事件时手动调用 OnBeginShutdown 方法。

The solution in the end was to hook into Microsoft.Win32.SystemEvents.SessionEnding and, when this System event was fired, to manually call the OnBeginShutdown method.

一抹微笑 2024-10-13 14:41:01

这曾经在 VB6 中导致 methode ~ of object ~ failed 错误。

尝试 WorkbookBeforeClose 或潜在的 ProtectedViewWindowBeforeClose。

如果我没记错的话,您可能会遇到一个问题,那就是您无法捕获事件取消的时间,因此,如果您使用它来进行清理,我相信您还需要在其中一项中做一些工作激活或打开事件,以便您的插件在用户取消关闭操作时可用......

希望这是有道理的。

This used to cause a methode ~ of object ~ failed error in VB6 days.

Try WorkbookBeforeClose or potentially ProtectedViewWindowBeforeClose.

One issue you may have with these if I remember correctly, is that you can't capture when if the event is cancelled, so if you're using this to clean up, I believe you need to also do some work in one of the activate or open events such that your addin will be useable if the user cancels the closing action....

Hope this makes sense.

恬淡成诗 2024-10-13 14:41:01

我的 Outlook 2010 插件也遇到同样的问题。这可能与 Outlook 2010 不这样做 向加载项发出正在关闭的信号

具体来说,Outlook [2010] 在快速关闭期间不再调用 IDTExtensibility2 接口的 OnBeginShutdownOnDisconnection 方法。

同样,当 Outlook 关闭时,使用 Microsoft Visual Studio Tools for Office 编写的 Outlook 加载项不再调用 ThisAddin_Shutdown 方法。

如果您仍然希望在 Outlook 2010 关闭时通知您的外接程序(就像我所做的那样),您需要使用代码锁定 ApplicationApplicationEvents_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...
}

I had the same problem with my Outlook 2010 addin. It may be something to do with the fact that Outlook 2010 does not signal add-ins that it is shutting down.

Specifically, Outlook [2010] no longer calls the OnBeginShutdown and OnDisconnection methods of the IDTExtensibility2 interface during fast shutdown.

Similarly, an Outlook add-in written with Microsoft Visual Studio Tools for Office no longer calls the ThisAddin_Shutdown method when Outlook is shutting down.

If you still want your addin to be notified when Outlook 2010 is shutting down (as I did), you need to latch on to the Application's ApplicationEvents_Event_Quit event, using code like mine below (your shutdown code should still run in both the OnDisconnection and OnBeginShutdown methods, in any case):

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...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文