Excel automation: Close event missing

做~自己de王妃 提交于 2019-12-01 18:20:20
chiccodoro

I've created a hack using a polling-like approach, and it works:

Given the workbook to observe, I create a thread which periodically tries to find that workbook in the workbooks collection.

(The DisposableCom class is my current solution to properly cleanup COM objects.)

Excel.Application app = wbWorkbook.Application;
string sWorkbookName = wbWorkbook.Name;

Thread overseeWorkbooksThread = new Thread(new ThreadStart(
    delegate()
    {
        bool bOpened = false;

        Excel.Workbooks wbsWorkbooks = app.Workbooks;
        using (new DisposableCom<Excel.Workbooks>(wbsWorkbooks))
        {
            while (true)
            {
                Thread.Sleep(1000);

                if (wbsWorkbooks.ContainsWorkbookProperly(sWorkbookName))
                    bOpened = true;
                else
                    if (bOpened)
                        // Workbook was open, so it has been closed.
                        break;
                    else
                    {
                        // Workbook simply not finished opening, do nothing
                    }
            }

            // Workbook closed
            RunTheCodeToBeRunAfterWorkbookIsClosed();
        }
    }));

overseeWorkbooksThread.Start();

The "ContainsWorkbookProperly" extension methods looks like this:

public static bool ContainsWorkbookProperly(this Excel.Workbooks excelWbs,
    string sWorkbookName)
{
    Excel.Workbook wbTemp = null;
    try
        wbTemp = excelWbs.Item(sWorkbookName);
    catch (Exception)
    {
        // ignore
    }

    if (wbTemp != null)
    {
        new DisposableCom<Excel.Workbook>(wbTemp).Dispose();
        return true;
    }

    return false;
}

Still I would be interested if there is a simpler or better solution.

This is not my code, but this worked a treat for me:

https://gist.github.com/jmangelo/301884

Copy paste:

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace Helpers.Vsto
{
    public sealed class WorkbookClosedMonitor
    {
        internal class CloseRequestInfo
        {
            public CloseRequestInfo(string name, int count)
            {
                this.WorkbookName = name;
                this.WorkbookCount = count;
            }

            public string WorkbookName { get; set; }

            public int WorkbookCount { get; set; }
        }

        public WorkbookClosedMonitor(Excel.Application application)
        {
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }

            this.Application = application;

            this.Application.WorkbookActivate += Application_WorkbookActivate;
            this.Application.WorkbookBeforeClose += Application_WorkbookBeforeClose;
            this.Application.WorkbookDeactivate += Application_WorkbookDeactivate;
        }

        public event EventHandler<WorkbookClosedEventArgs> WorkbookClosed;

        public Excel.Application Application { get; private set; }

        private CloseRequestInfo PendingRequest { get; set; }

        private void Application_WorkbookDeactivate(Excel.Workbook wb)
        {
            if (this.Application.Workbooks.Count == 1)
            {
                // With only one workbook available deactivating means it will be closed
                this.PendingRequest = null;

                this.OnWorkbookClosed(new WorkbookClosedEventArgs(wb.Name));
            }
        }

        private void Application_WorkbookBeforeClose(Excel.Workbook wb, ref bool cancel)
        {
            if (!cancel)
            {
                this.PendingRequest = new CloseRequestInfo(
                    wb.Name, 
                    this.Application.Workbooks.Count);
            }
        }

        private void Application_WorkbookActivate(Excel.Workbook wb)
        {
            // A workbook was closed if a request is pending and the workbook count decreased
            bool wasWorkbookClosed = true
                && this.PendingRequest != null
                && this.Application.Workbooks.Count < this.PendingRequest.WorkbookCount;

            if (wasWorkbookClosed)
            {
                var args = new WorkbookClosedEventArgs(this.PendingRequest.WorkbookName);

                this.PendingRequest = null;

                this.OnWorkbookClosed(args);
            }
            else
            {
                this.PendingRequest = null;
            }
        }

        private void OnWorkbookClosed(WorkbookClosedEventArgs e)
        {
            var handler = this.WorkbookClosed;

            if (handler != null)
            {
                handler(this, e);
            }
        }
    }

    public sealed class WorkbookClosedEventArgs : EventArgs
    {
        internal WorkbookClosedEventArgs(string name)
        {
            this.Name = name;
        }

        public string Name { get; private set; }
    }
}

When I used it I changed it from return the name of the workbook to a reference to the workbook.

Can you use both events? On BeforeClose() set a flag, then BeforeSave() see if the flag is set. You would need a way to reset it, though, in case BeforeClose() is triggered and BeforeSave() isn't. Not sure if there is something else that could help with that.

Edit: Looks like you covered this already with "the exact same order of events is executed". But if you can find a way to reset it (another "Cancel" event?) it may work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!