{"id":5500,"date":"2014-03-30T23:14:52","date_gmt":"2014-03-30T23:14:52","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/save-as-event-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T23:14:52","modified_gmt":"2014-03-30T23:14:52","slug":"save-as-event-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/save-as-event-in-c-collection-of-common-programming-errors\/","title":{"rendered":"save as event in C#-Collection of common programming errors"},"content":{"rendered":"<p>The Application.WorkbookAfterSave event passes in a WorkBook object named <code>wb<\/code> and a boolean named <code>Success<\/code>.<\/p>\n<p>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:<\/p>\n<pre><code>using Excel = Microsoft.Office.Interop.Excel;\nusing System;\n\nnamespace ExcelAddIn1\n{\n    public partial class ThisAddIn\n    {\n        private string CurrentFullName { get; set; }\n        private event EventHandler SaveAs;\n\n        private void ThisAddIn_Startup(object sender, System.EventArgs e)\n        {\n            CurrentFullName = this.Application.ThisWorkbook.FullName;\n\n            this.Application.WorkbookAfterSave += new Excel.AppEvents_WorkbookAfterSaveEventHandler(Application_WorkbookAfterSave);\n\n            SaveAs += new EventHandler(ThisAddIn_SaveAs);\n        }\n\n        void ThisAddIn_SaveAs(object sender, EventArgs e)\n        {\n            \/\/Do What I want to do if saved as\n        }\n\n        void Application_WorkbookAfterSave(Excel.Workbook Wb, bool Success)\n        {\n            if (Wb.FullName != CurrentFullName)\n            {\n                CurrentFullName = Wb.FullName;\n                SaveAs.Invoke(null, null);\n            }\n        }\n\n        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)\n        {\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5500","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5500"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5500\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}