我使用VSTO(C#)为excel开发了一个插件。
我想知道工作簿在运行时是否“ SAVED AS ”。即。用户在WBK 1上执行某些操作,然后使用SAVE AS保存工作簿,然后重命名工作簿并再次开始处理它。那么在这种情况下是否可以捕获'Worlbook SAve AS'事件?
答案 0 :(得分:5)
Application.WorkbookAfterSave事件传入名为wb
的WorkBook对象和名为Success
的布尔值。
您可以从Workbook对象的FullName属性中获取保存为文件名。也许您可以将初始名称存储在变量中,并在AfterSave事件上比较名称和位置,以查看是否已按原样使用“另存为”:
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)
{
}
}
}