2

VSTO(C#)を使用してExcel用のアドインを開発しました。

実行時にワークブックが「保存済み」である場合はいつでも知りたいです。すなわち。ユーザーがWBK1で何らかの操作を実行しているときに、SAVE ASを使用してワークブックを保存し、ワークブックの名前を変更して、作業を再開します。したがって、この場合、「Worlbook SAveAS」イベントをキャッチすることは可能ですか?

4

1 に答える 1

6

The Application.WorkbookAfterSave event passes in a WorkBook object named wb and a boolean named Success.

You can get the saved as filename from the FullName property of the Workbook object. Maybe you could store the initial name in a variable, and on the AfterSave event compare the names and locations to see if Save As has been used like so:

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 に答える