Word 2007 加载项功能区 - InternalStartup

Word 2007 Addin Ribbon - InternalStartup

对于 Visual Studio 2013,我以 .NET Framework 4 为目标,并创建了一个同时以 Office 2007 和 Office 2010 为目标的加载项。我选择了 2010 加载项,但它应该可以在 2007 年工作link: https://blogs.msdn.microsoft.com/vsto/2010/06/04/creating-an-add-in-for-office-2007-and-office-2010-that-lights-up-on-office-2010-mclean-schofield/

我用的是ribbon designer(不是ribbonxml)在我的AddIn中我的启动方法是这样的:

private void InternalStartup()
{
  this.Startup += new System.EventHandler(ThisAddIn_Startup);
  this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
  ((Word.ApplicationEvents4_Event)this.Application).NewDocument += new Microsoft.Office.Interop.Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
  this.Application.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);

//THIS LINE FAILS IN Word 2007 but not in Word 2010
this.Application.ActiveDocument.Saved = false;
}

基本上我正在捕获关闭事件,并在该事件中编写了一些我自己的自定义代码。这在 2010 年完美运行。在 2007 年,功能区安装并且某些功能正常工作,但是当我关闭文档时,我的 'Application_DocumentBeforeClose' 关闭事件在 Word 2007 中没有被调用。有什么建议吗?

更新:我按照建议更改了我的代码,但是当我打开文档时出现错误,以下行在 Word 2007 中失败 - 'This command is not available because no document is open.'

this.Application.ActiveDocument.Saved = false;

看看类似的论坛帖子 - Word DocumentBeforeClose not firing。内容如下:

If we open a document, Word will detect whether the new document is modifed. If it is the blank document from scratch, Word uses it to host the target document directly. So in this case, it is not considered a Close action. Hence, the DocumentBeforeClose does not fire.