save as event in C#-Collection of common programming errors
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)
{
}
}
}