Page 1 of 1

What is the correct way of closing an excel workbook using COM?

Posted: 26 Jul 2021, 10:34
by fenchai

Code: Select all

xl := ComObjCreate("Excel.Application") ; init excel instance
xl.Visible := false ; set instance to not be visible

wb := xl.Workbooks.Open(filePath) ; open wb

; do whatever I want to do

; wb.Close(False) throws me an error about invalid command or something

xl.Quit() ; exit instance (this is not working, when I try to open the file again it throws me into read more only)

Re: What is the correct way of closing an excel workbook using COM?  Topic is solved

Posted: 26 Jul 2021, 11:54
by boiler
Close any open Excel processes via task manager, then run your script again with wb.Close() and/or xl.Quit().

Re: What is the correct way of closing an excel workbook using COM?

Posted: 26 Jul 2021, 22:25
by flyingDman
wb,close(0) or wb.close() will close the workbook without saving. wb.close(1) saves and closes the workbook. Any of these will not quit excel. For that, xl.quit() is needed.
See here https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.close