c++ - C# program for automating excel with before close event handler not saving edited excel file -
c++ - C# program for automating excel with before close event handler not saving edited excel file -
i wanted avoid excel ole calls through c++ code launch excel. wanted wait excel closed before proceeding. implemented next c# programme , utilize scheme phone call execute it. works changes create excel not getting saved. not sure whats happening. can please help.
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using excel = microsoft.office.interop.excel; using system.io; using system.runtime.interopservices; using system.windows.forms; namespace excelhandler { class programme { private static bool isclosed = false; private static void app_workbookbeforeclose(excel.workbook wb, ref bool cancel) { if (!wb.saved) { dialogresult result = messagebox.show("do want save " + "changes made " + wb.fullname + "?", "blizzard excel", messageboxbuttons.yesnocancel,messageboxicon.warning, messageboxdefaultbutton.button1, messageboxoptions.defaultdesktoponly); switch (result) { case dialogresult.yes: wb.save(); break; case dialogresult.cancel: cancel = true; break; // next code ensures default save file // dialog not displayed. case dialogresult.no: wb.saved = true; break; } } isclosed = !cancel; // wb.parent } static int main(string[] args) { int returnvalue = 0; if (args.length != 1) { console.writeline("-e- excelhandler takes single xlxs input"); homecoming -1; } string inputfile = args[0]; if (!file.exists(inputfile)) { console.writeline("-e- input file doesn't exist: {0}", inputfile); homecoming -1; } excel.application xlapp = new excel.application(); seek { excel.workbook xlworkbook = xlapp.workbooks.open(inputfile); xlapp.visible = true; excel.appevents_workbookbeforecloseeventhandler event_beforebookclose; event_beforebookclose = new excel.appevents_workbookbeforecloseeventhandler(app_workbookbeforeclose); xlapp.workbookbeforeclose += event_beforebookclose; // || while (!isclosed) { system.threading.thread.sleep(300); } //xlworkbook.close(true); marshal.releasecomobject(xlworkbook); } grab { } { xlapp.quit(); marshal.releasecomobject(xlapp); } homecoming returnvalue; } } }
i found issue. messageboxoptions.defaultdesktoponly
alternative in
dialogresult result = messagebox.show("do want save " + "changes made " + wb.fullname + "?", "blizzard excel", messageboxbuttons.yesnocancel,messageboxicon.warning, messageboxdefaultbutton.button1, messageboxoptions.defaultdesktoponly);
i changed next working.
nativewindow nw = new system.windows.forms.nativewindow(); nw.assignhandle(new intptr(wb.parent.hwnd)); dialogresult result = messagebox.show(nw,"do want save " + "changes made " + wb.fullname + "?", "blizzard excel", messageboxbuttons.yesnocancel, messageboxicon.warning, messageboxdefaultbutton.button1);
i still not sure wrong initial code.
c# c++ excel event-handling
Comments
Post a Comment