Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Word加载项DocumentBeforeClose事件仅触发一次_C#_Events_Ms Word_Vsto - Fatal编程技术网

C# Word加载项DocumentBeforeClose事件仅触发一次

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); 我可以处

我正在用c#编写Word应用程序外接程序

我正在尝试在关闭前使用
文档
事件:

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
}