简体   繁体   English

自定义 Microsoft Word .NET VSTO 加载项在闲置一小时后中断

[英]Custom Microsoft Word .NET VSTO Add-In breaks after an hour of idling

We have a custom Ribbon built to work with Microsoft Word that utilizes Word's DocumentBeforeSave event to execute some business logic every time the user saves the document, binding and declarations look like this:我们有一个自定义功能区,用于与 Microsoft Word 一起使用,它利用 Word 的 DocumentBeforeSave 事件在每次用户保存文档时执行一些业务逻辑,绑定和声明如下所示:

//This binding is executed on Ribbon load:
Globals.ThisAddIn.Application.DocumentBeforeSave += Application_DocumentBeforeSave;


//This is the method we bind to the event:
void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
{
     // Business logic here
}

This works perfectly fine, and the Doc object being passed to our method contains all the content and relevant data (notes, path, name, etc...) However if the application is left to idle for about an hour the function no longer receives the data as expected.这工作得很好,传递给我们方法的Doc object 包含所有内容和相关数据(注释、路径、名称等)但是,如果应用程序闲置大约一个小时,function 将不再接收数据符合预期。

Results of adding logs and tracking the process at failure shows the following:添加日志并跟踪失败过程的结果显示如下:

  1. The Application_DocumentBeforeSave function is still properly bound to the correct event and is being fired every time the user saves Application_DocumentBeforeSave function 仍然正确绑定到正确的事件,每次用户保存时都会被触发
  2. The Word.Document Doc variable being passed to Application_DocumentBeforeSave however does not contain the content of the active document or any of the relevant data.然而,传递给Application_DocumentBeforeSaveWord.Document Doc变量不包含活动文档的内容或任何相关数据。 Debugging the Doc.Path member returns the following path: C:\Users\<USER>\AppData\Roaming\Microsoft\Word instead of the expected path of the open document.调试Doc.Path成员返回以下路径: C:\Users\<USER>\AppData\Roaming\Microsoft\Word而不是打开文档的预期路径。

Is this a known issue of VSTO AddIns timing out or is there a way to prevent this from happening?这是 VSTO 加载项超时的已知问题,还是有办法防止这种情况发生?

Is this a known issue of VSTO AddIns timing out or is there a way to prevent this from happening?这是 VSTO 加载项超时的已知问题,还是有办法防止这种情况发生?

Nope.没有。 The Application.DocumentBeforeSave event is fired before any open document is saved. Application.DocumentBeforeSave事件在保存任何打开的文档之前触发。 That means it is fired for all documents opened in Word.这意味着它会为在 Word 中打开的所有文档触发。 For example, Word can be automated from another applications and etc.例如,Word 可以从其他应用程序等自动化。

It is not clear what code is used in the event handler itself.目前尚不清楚事件处理程序本身使用了哪些代码。 And it would great to check other add-ins and code that could handle the same event as well.检查其他也可以处理相同事件的加载项和代码会很棒。 They can release the document instance, for example.例如,他们可以发布文档实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM