C# Word加载项DocumentBeforeClose事件仅触发一次
我正在用c#编写Word应用程序外接程序 我正在尝试在关闭前使用C# Word加载项DocumentBeforeClose事件仅触发一次,c#,events,ms-word,vsto,C#,Events,Ms Word,Vsto,我正在用c#编写Word应用程序外接程序 我正在尝试在关闭前使用文档事件: Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument); vstoDoc.BeforeClose += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforeClose); 我可以处
文档
事件:
Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
vstoDoc.BeforeClose += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforeClose);
我可以处理它,但当我尝试关闭和打开文档(而不是整个应用程序)时,我无法再处理此事件
编辑:
关闭前的文档很好
但是我如何使用ref bool Cancel呢
我尝试:
void Application_DocumentBeforeClose(_Word.Document Doc, ref bool Cancel)
{
Cancel = true;
}
但是它不起作用:(您注册的事件处理程序位于一个特定文档上。如果您关闭它,则事件处理程序不计入后续文档 如果不希望在关闭前触发某个特定文档的
事件,但对于打开的任何文档,请使用该事件
Globals.ThisAddIn.Application.DocumentBeforeClose += Application_DocumentBeforeClose;
void Application_DocumentBeforeClose(_Word.Document Doc, ref bool Cancel)
{
// Doc is the document getting closed
}