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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fenchai
Posts: 290
Joined: 28 Mar 2016, 07:57

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

Post by fenchai » 26 Jul 2021, 10:34

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)

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

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

Post by boiler » 26 Jul 2021, 11:54

Close any open Excel processes via task manager, then run your script again with wb.Close() and/or xl.Quit().

User avatar
flyingDman
Posts: 2776
Joined: 29 Sep 2013, 19:01

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

Post by flyingDman » 26 Jul 2021, 22:25

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
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”