使用 autohotkey 保存并关闭所有 excel 个文件

Save and close all excel files with autohotkey

我正在尝试创建一个 ahk 脚本来保存和关闭我打开的所有 excel 个文件,但是我正在努力。

我试过了,但在两行中出现错误(见下文):

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

我试过了,但我又遇到错误:

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

有人可以帮忙吗?提前致谢。

如果循环中有多个语句(命令、赋值、函数调用等),loop 命令后必须跟一个 {...} 块:

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