如何设置 (Word/Excel).Document.BeforeCloseEvent (使用 Interop)以防止它们被关闭?

How set a (Word/Excel).Document.BeforeCloseEvent (using Interop) to prevent them being closed?

我有一个 Windows.Form application,它与 WordExcel 一起运行。为了与他们合作,我使用 Interop package。另外,我正在使用 Visual Studio 和 C# languaje 进行编程。

我创建了一个 EventHandler,它在 Word 文档关闭之前执行,另一个 EventHandler 在 Excel 文档关闭之前执行。这两个事件的作用是:取消关闭文档,并隐藏它而不是关闭,所以文档在内存中继续运行。

ExcelDocument->BeforeCloseEvent 工作正常但 WordDocument->BeforeCloseEvent 给出错误:

System.NullReferenceException: Object reference not set as an instance of an object.

我得到错误的行在下面的代码中指出。 (在代码中,我只展示了如何包含 Word/Excel、启动它们以及两个 EventHandlers...在完整代码的其他部分,我展示了使用 aplicacionWord/aplicacionExcel .visible = true,.. .)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
// Include Word
using Word = Microsoft.Office.Interop.Word;
// Include Excel
using Excel = Microsoft.Office.Interop.Excel;

public partial class formWord : Form
{
    // New object type application of Word
    Word.Application aplicacionWord = new Word.Application();
    // New instance for Word document
    Word.Document documentoWord;
    // New object type application of Excel
    Excel.Application aplicacionExcel = new Excel.Application();
    // New instance for Excel document
    Excel.Workbook documentoExcel;
    // New instance for Excel Worksheet
    Excel.Worksheet documentoExcelPrimeraHoja;

    private void formWord_Load(object sender, EventArgs e)
    {
        // Word application is not visible
        aplicacionWord.Visible = false;
        // Create new Word document
        documentoWord = aplicacionWord.Documents.Add();
        // Excel application is not visible
        aplicacionExcel.Visible = false;
        // Create new document and get its first worksheet
        documentoExcel = aplicacionExcel.Workbooks.Add(Missing.Value);
        documentoExcelPrimeraHoja = (Excel.Worksheet)documentoExcel.Worksheets.get_Item(1);

        // Word document event before close
        aplicacionWord.DocumentBeforeClose +=
            new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(
                (Word.Document Doc, ref bool Cancel) => {
                    // If Word aplication is visible, then:
// HERE IS THE ERROR ---------------------------------
// ------- in aplicacionWord --> NULL REFERENCE
                    if (aplicacionWord.Visible)
                    {
                        // Cancel closing document
                        Cancel = true;
                        // Hide againg Word application
                        aplicacionWord.Visible = false;
                    }
                }
            );

        // Excel document event before close
        documentoExcel.BeforeClose +=
            new Excel.WorkbookEvents_BeforeCloseEventHandler(
                (ref bool Cancel) => {
                    // If Excel aplication is visible, then:
                    if (aplicacionExcel.Visible)
                    {
                        // Cancel closing document
                        Cancel = true;
                        // Hide Excel aplication again
                        aplicacionExcel.Visible = false;
                    }
                }
            );
    }
}

// How I close both documents
private void formWord_FormClosing(object sender, FormClosingEventArgs e)
{

    // Closing applications
    aplicacionWord.Quit();
    aplicacionWord= null;
    aplicacionExcel.Quit();
    aplicacionExcel = null;
}

正如我所说,我在 Excel 的事件中没有发现任何错误,但在 Word 中却出现了错误。错误是由于文档正在关闭,所以... BeforeClose 没有按预期工作。还有其他方法可以处理 BeforeClose 事件吗?还是我做错了什么?

此外,我不确定这是否是处理 Word 和 Excel 的 Document.BeforeCloseEvent 的最佳方式。任何升级这个的想法都会很棒。

提醒:我希望当 Word/Excel 文档关闭时 - Word/Excel 应用程序可见 - (用户单击 Top-Right-X 退出)而不是关闭文档,隐藏所以它保持打开状态。

PS:如果你想在你的机器上尝试这个问题,你只需在我的代码中更改它:

// Word application is not visible
aplicacionWord.Visible = true;
// Excel application is not visible
aplicacionExcel.Visible = true;

然后,当 c# 应用程序为 运行 时,关闭两个程序 (Word/Excel),您将看到错误。

我只能用 Word 说话 - 但因为那是你遇到错误的地方......你说

Reminder: I want that when a Word/Excel document is closing - the Word/Excel application is visible - (user clicks on Top-Right-X to exit) instead of closing the document, hide it so it remains open.

如果用户单击应用程序的红色 x 按钮,那么他不是在关闭文档,而是在退出 Word.Application。这也将关闭触发 BeforeClose 的文档,但由于该应用程序不再可用,您会收到错误消息 - 对象 aplicacionWord 是 "null".

Word 有一个 Quit 事件,但没有提供取消选项 - Word 将退出。

您可以将代码放入 Quit 事件中,但是,要执行某些操作,例如设置布尔值,您可以在 BeforeClose 中签入、保存文档、启动 Word 的新实例- 不可见 - 并在新实例中再次打开文档。

请务必注意,Excel 和 Word 在很多方面并不。尽管两者都在 "Microsoft Office" 的保护伞下,但它们的起源截然不同,并且是由不同的团队开发的。偶尔会尝试将所有 "core" MS Office 应用程序放在一起,因为它们确实有一些共同点(打开、保存和关闭用户界面就是一个典型的例子),但它们的行为方式 "internally" 完全独立且无关。