1

When the application closes, the display pops up if I want to save the file. I don't want to save the file and want it to automatically close.

I thought DisplayAlerts = False would do the job, but it won't work

if os.path.exists("file_name.xlsm"):
    xl=win32com.client.Dispatch("Excel.Application")
    xl.Workbooks.Open(os.path.abspath("file_name.xlsm"))
    xl.Application.Run("module.module")
    sht.range('A1').value = 11
    xl.DisplayAlerts = False 
    xl.Application.Quit() 
    del xl
fuglede
  • 17,388
  • 2
  • 54
  • 99
Bart
  • 11
  • 1

1 Answers1

0

From the documentation:

If unsaved workbooks are open when you use this method, Microsoft Excel displays a dialog box asking whether you want to save the changes. You can prevent this by saving all workbooks before using the Quit method or by setting the DisplayAlerts property to False. When this property is False, Microsoft Excel doesn't display the dialog box when you quit with unsaved workbooks; it quits without saving them.

If you set the Saved property for a workbook to True without saving the workbook to the disk, Microsoft Excel will quit without asking you to save the workbook.

That is, what you're doing should work, and indeed works completely fine in my testing (just opening Excel through COM, setting xl.Visible to True, creating a new workbook, changing it, and trying to close the application with and without DisplayAlerts set to False).

From here, you could try to

  • set Saved to True for each open workbook, as suggested by the documentation, or
  • let Application.EnableEvents = False,

prior to quitting.

Community
  • 1
  • 1
fuglede
  • 17,388
  • 2
  • 54
  • 99