Save and close all excel files with autohotkey

Viewed 266

I'm trying to create an ahk script to save and close all excel files I have open, however I'm struggling.

I've tried this but get errors on two lines (see below):

Excel := ComObjActive("Excel.Application")
loop % Excel.workbooks.count ;i get an error on this line
    if fileexist(Excel.workbooks(a_index).fullname)
        Excel.Workbooks(a_index).save
excel.quit() ;i get an error on this line

And I've tried this, but again I get errors:

xl := ComObjActive("Excel.Application")
for wrkbk in xl.workbooks ;i get an error on this line
    wrkbk.close(1)
xl.quit() ;i get an error on this line

Can anyone help? Thanks in advance.

1 Answers

The loop command has to be followed by a {...} block if you have more than one statements (commands, assignments, function calls etc.) within the loop:

Excel := ComObjActive("Excel.Application")
loop % Excel.workbooks.count
{
    if fileexist(Excel.workbooks(a_index).fullname)
        Excel.Workbooks(a_index).save
}
excel.quit() 
Related