我尝试最小化并关闭DocumentBeforeClose事件方法中的文档。但如果我这样做,那么这个词就有可能崩溃。并非每次都可重现。
我构建了一个非常简化的插件版本。它只包含事件的注册和事件方法本身:
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
//Apply Application
m_ApplicationObject = (Microsoft.Office.Interop.Word.Application)application;
//Register event
m_ApplicationObject.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(m_ApplicationObject_DocumentBeforeClose);
}
private void m_ApplicationObject_DocumentBeforeClose(Microsoft.Office.Interop.Word.Document Doc, ref bool cancel)
{
//Close Document. If you do so, the Word-Window will stay open! WINWORD.EXE will not exit!
object doNotSaveChanges = MSword.WdSaveOptions.wdDoNotSaveChanges;
object missing = System.Reflection.Missing.Value;
Doc.Close(ref doNotSaveChanges, ref missing, ref missing);
//Minimize Word-Window.
m_ApplicationObject.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;
}
崩溃出现在Word 2016上。我无法测试旧版本。
如何重现: 打开随机word文档。点击' X'关闭文件将结束,单词将最小化。恢复单词并打开随机文档,点击' X'关闭...重复2-5次,然后发生崩溃。