prompt saving on close multiple times
I am using VS2008. In my application, from a MMC snap-in I run an Excel 2003 Worksheet (in Office 2007) so that the user can modify certain parameters.
The code is subscribed to the BeforeClosing Event, Window Deactivate and WindowActivate. Everything runs smoothly only after the user chooses to close the Excel app, he is prompted to save the file yes/no/cancel, and the sheet is never/always saved. Problem is that I am using Workbook.Close() to actually close the damn thing. It's first parameter is a bool if it's on true it will save everytime, on false never, and on Type.missing it will prompt again on close. So the user is prompted once (this one has only the cancel functionality) and then he is prompted again by the Workbook.Close() to save (this one has yes/no functionality but no cancel functionality - it will close even if you click cancel).
No I've tried to subscribe to the BeforeSave event and tried to put that in the beforeClose but it doesn't fire; Also force saving is not a solution and skip closing the workbook and kill excel (to avoid workbook close) is also not a solution.
I've also tried to avoid workbook.close by simply releasing the com object associated to the workbook, but to no avail.
One thing to note, if you click file-save, a dialog box appears informing you that if you save the spreadsheet some functionality may not be available in office 2003 (could that extra dialog box be a problem?)
One potential solution (one that I would have as a last resort) would be to supress the save prompt on close and fire my own dialog box from code (a dialog box that I can control) and 开发者_Python百科read the result and do things accordingly (I want to avoid this because it has to be translated in 20+ languages and takes a lot of time...)
I am at a loss for solutions, if you have any ideas i would be very grateful.
SOLVED:
A colleague of mine finally cracked it... Apparently there were some issues when direcly closing Excel, so with a rudimentary approach of delegating a Thread to sleep 1 second and then killing the Excel app, it was able to save (if it was requested by the user) and successfully exit.
You can also set Interactive property to false. This will prevent XL from showing any dialog.
精彩评论