Closing open Excel files safely without saving them Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Closing open Excel files safely without saving them

26 Feb 2018, 13:24

I understand the following code works to close Excel files and saving them ( http://www.autohotkey.com/board/topic/8 ... workbooks/ ). However, I would like to close several files without saving any of them. Any suggestions? PS: In the past, I force-closed Excel processes and did some damage. I had to reinstall the Excel.

Code: Select all

Process, Exist, EXCEL.EXE
If !ErrorLevel
Return
Loop
{
XL := ComObjActive("Excel.Application")
For Book in XL.Workbooks
Book.Close(1)
XL.Quit(), XL := ""
Process, Exist, EXCEL.EXE
} Until !ErrorLevel
Last edited by carno on 26 Feb 2018, 13:34, edited 1 time in total.
Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

Re: Closing open Excel files safely without saving them  Topic is solved

26 Feb 2018, 13:33

carno wrote:I understand the following code works to close Excel files and saving them ( http://www.autohotkey.com/board/topic/8 ... workbooks/ ). However, I would like to close several files without saving any of them. Any suggestions?
Remove the 1 from the close line, as that is passing True to save changes. (https://msdn.microsoft.com/en-us/vba/ex ... thod-excel)

Code: Select all

Process, Exist, EXCEL.EXE
If !ErrorLevel
Return
Loop
{
XL := ComObjActive("Excel.Application")
For Book in XL.Workbooks
Book.Close() ; Remove the 1 from this line.
XL.Quit(), XL := ""
Process, Exist, EXCEL.EXE
} Until !ErrorLevel
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Closing open Excel files safely without saving them

26 Feb 2018, 13:40

Thanks, I hope this will not cause any damages. (In the past, I force-closed Excel processes and did some damage. I had to reinstall the Excel.)

Code: Select all

Process, Close, Excel.exe
:shock:
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Closing open Excel files safely without saving them

26 Feb 2018, 16:47

Microsoft site (https://msdn.microsoft.com/en-us/vba/ex ... thod-excel) states:
Example:
This example closes Book1.xls and discards any changes that have been made to it.

Code: Select all

Workbooks("BOOK1.XLS").Close SaveChanges:=False
I believe this is the opposite. I tested many times with a workbook macro and False actually SAVES any changes and True DISCARDS (does not save any changes).
https://msdn.microsoft.com/en-us/VBA/Ex ... erty-excel
Workbook.Saved Property (Excel)
You can set this property to True if you want to close a modified workbook without either saving it or being prompted to save it.
https://autohotkey.com/board/topic/1278 ... ut-saving/
Another option is to simply set the Workbook.Saved property to True, in which case Excel will act as if the workbook was already saved and there are no new changes, so it will not save it when you close it.
https://support.microsoft.com/en-us/hel ... k-in-excel
How to suppress "Save Changes" prompt when you close a workbook in Excel

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AHK_user, sharonhuston and 222 guests