2

我使用 VSTO(C#) 为 excel 开发了一个插件。

我想知道工作簿在运行时是否“另存为”。IE。用户正在 WBK 1 上执行一些操作,然后他使用 SAVE AS 保存工作簿,然后重命名工作簿并再次开始处理它。那么在这种情况下是否有可能捕捉到“Worlbook SAve AS”事件?

4

1 回答 1

6

Application.WorkbookAfterSave 事件传入一个名为的 WorkBook 对象wb和一个名为 的布尔值Success

您可以从 Workbook 对象的 FullName 属性中获取保存为文件名。也许您可以将初始名称存储在变量中,并在 AfterSave 事件中比较名称和位置,以查看是否已像这样使用 Save As:

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

namespace ExcelAddIn1
{
    public partial class ThisAddIn
    {
        private string CurrentFullName { get; set; }
        private event EventHandler SaveAs;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            CurrentFullName = this.Application.ThisWorkbook.FullName;

            this.Application.WorkbookAfterSave += new Excel.AppEvents_WorkbookAfterSaveEventHandler(Application_WorkbookAfterSave);

            SaveAs += new EventHandler(ThisAddIn_SaveAs);
        }

        void ThisAddIn_SaveAs(object sender, EventArgs e)
        {
            //Do What I want to do if saved as
        }

        void Application_WorkbookAfterSave(Excel.Workbook Wb, bool Success)
        {
            if (Wb.FullName != CurrentFullName)
            {
                CurrentFullName = Wb.FullName;
                SaveAs.Invoke(null, null);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
    }
}
于 2012-07-23T11:37:39.217 回答