2014-11-24 82 views
0

我正在一个Word应用程序加载在C#。Word加载DocumentBeforeClose事件只发射一次

我试图使用DocumentBeforeClose事件:

Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument); 

vstoDoc.BeforeClose += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforeClose); 

我能应付,但是当我尝试关闭并打开一个文件(不是整个应用程序),我无法处理此事件的任何更多。

编辑:

DocumentBeforeClose很好。

但如何,我可以使用参考BOOL取消

我尝试:

void Application_DocumentBeforeClose(_Word.Document Doc, ref bool Cancel) 
{ 
    Cancel = true; 
} 

但其doestn工作:(

回答

0

你注册的事件处理程序是在一个特定的文件如果关闭。它的事件处理程序不计后续文件。

如果你不想BeforeClose事件火了一个特定的文件,但对于任何打开的文档都使用Application.DocumentBeforeClose事件。

Globals.ThisAddIn.Application.DocumentBeforeClose += Application_DocumentBeforeClose; 

void Application_DocumentBeforeClose(_Word.Document Doc, ref bool Cancel) 
{ 
    // Doc is the document getting closed 
}