【问题标题】:Word add-in DocumentBeforeClose event only firing onceWord 加载项 DocumentBeforeClose 事件仅触发一次
【发布时间】:2015-03-07 03:35:23
【问题描述】:

我正在用 c# 编写 Word 应用程序插件。

我正在尝试使用DocumentBeforeClose 事件:

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

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

我可以处理它,但是当我尝试关闭和打开一个文档(不是整个应用程序)时,我无法再处理这个事件了。

编辑:

DocumentBeforeClose 很好。

但是我如何使用 ref bool Cancel

我试试:

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

但它不起作用:(

【问题讨论】:

    标签: c# events ms-word vsto


    【解决方案1】:

    您注册的事件处理程序位于一个特定文档上。如果关闭它,则事件处理程序不计入后续文档。

    如果您不想为某个特定文档触发 BeforeClose 事件,但对于任何打开的文档,请使用 Application.DocumentBeforeClose 事件。

    Globals.ThisAddIn.Application.DocumentBeforeClose += Application_DocumentBeforeClose;
    
    void Application_DocumentBeforeClose(_Word.Document Doc, ref bool Cancel)
    {
        // Doc is the document getting closed
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 2023-03-21
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多