Excel 插件 - 注销时未调用 OnDisconnection/OnBeginShutdown
我在 Visual Studio 2008 中使用可扩展性 IDTExtensibility2 接口开发了一个 Excel 共享插件。
它的功能非常基本。当工作簿打开时,它存储在打开的工作簿列表中,当关闭时,插件将创建一个新的文本文件,然后将一些 XML 写入该文件,然后该文件由另一个进程读取,然后该进程将反序列化 XML 。
该插件在正常操作下工作 - 因此,如果用户打开并关闭文件,那么插件会执行它应该执行的操作,如果他们退出 Excel 并打开工作簿,那么它会执行它应该执行的操作。
问题是当用户打开 Excel 并打开工作簿并注销时。这两个方法:OnDisconnection 和 OnBeginShutdown 似乎根本没有被调用。
我做了两件事来测试这一点:
我创建了一个 TextWriterTraceListener,它在调用这两个方法时写入日志文件。当 Excel 正常退出时,它们会被命中,信息会记录在日志文件中,但是当用户注销时,日志文件中没有任何内容。
在这两种方法中,我使用 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:
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最终的解决方案是挂钩 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 theOnBeginShutdown
method.这曾经在 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.
我的 Outlook 2010 插件也遇到同样的问题。这可能与 Outlook 2010 不这样做 向加载项发出正在关闭的信号。
如果您仍然希望在 Outlook 2010 关闭时通知您的外接程序(就像我所做的那样),您需要使用代码锁定
Application
的ApplicationEvents_Event_Quit
事件就像下面我的一样(无论如何,您的关闭代码仍应在OnDisconnection
和OnBeginShutdown
方法中运行):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.
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
'sApplicationEvents_Event_Quit
event, using code like mine below (your shutdown code should still run in both theOnDisconnection
andOnBeginShutdown
methods, in any case):