【发布时间】:2023-01-25 07:37:08
【问题描述】:
我们有一个自定义功能区,用于与 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
}
这工作得很好,传递给我们方法的 Doc 对象包含所有内容和相关数据(注释、路径、名称等...)但是,如果应用程序闲置大约一个小时,函数 no longer 按预期接收数据。
添加日志并跟踪失败过程的结果显示如下:
-
Application_DocumentBeforeSave函数仍然正确绑定到正确的事件,每次用户保存时都会被触发 - 传递给
Application_DocumentBeforeSave的Word.Document Doc变量不包含活动文档的内容或任何相关数据。调试Doc.Path成员返回以下路径:C:\Users\<USER>\AppData\Roaming\Microsoft\Word而不是打开文档的预期路径。这是 VSTO 加载项超时的已知问题,还是有办法防止这种情况发生?
【问题讨论】:
-
这可以是其他文件吗?如果您明确修改并保存文档,您是否获得了正确的文档?
标签: c# ms-word vsto office-addins word-addins