Autohotkey运行Excel VBAmacros,但不会应用更改

我有一个Excel文件,说Plano.xlsx ,我试图按照这里说明的说明使用Autohotkey上运行VBAmacros脚本。 我不希望在这个过程中看到Excel。 VBA代码应该在第一个表单元格C1中input值99。 经过数小时的反复试验,Autohotkey脚本运行平稳无误,即在后台打开一个Excel进程,据说编辑Excel文件,然后退出。 问题是Excel文件根本没有改变。 VBA代码工作正常,如果我手动将其粘贴到Excel中的新VBA模块,而不使用Autohotkey。

这里是代码:

 #SingleInstance force #Include Acc.ahk VBcode= ( Sub myFunction() Worksheets(1).Select Worksheets(1).Range("C1").Select Selection.Value = 99 End Sub ) Excel_Run("myFunction") Excel_Run(sFunction){ FilePath = C:\Users\KostasK\Desktop\Plano.xlsx oExcel := ComObjCreate("Excel.Application") Excel_ImportCode(VBcode) oWorkbook := oExcel.Workbooks.Open(FilePath) Excel_Get().Run(sFunction) oWorkbook.Save oExcel.Quit } Excel_ImportCode(VBcode){ if fileexist(A_ScriptDir . "\tempvbcode.txt") FileDelete, %A_ScriptDir%\tempvbcode.txt FileAppend, %VBcode%, %A_ScriptDir%\tempvbcode.txt Excel_Get().ActiveWorkbook.VBProject .VBComponents.Import(A_ScriptDir . "\tempvbcode.txt") } Excel_Get(WinTitle="ahk_class XLMAIN") { ; by Sean and Jethrow, minor modification by Learning one ControlGet, hwnd, hwnd, , Excel71, %WinTitle% if !hwnd return Window := Acc_ObjectFromWindow(hwnd, -16) Loop try Application := Window.Application catch ControlSend, Excel71, {esc}, %WinTitle% Until !!Application return Application } 

要获得脚本中包含的Acc.ahk库,请参阅此处 。 我的Autohotkey版本是v.1.1.23.05,我使用Excel 2013.我没有仔细看看Excel_Get()函数,但是我用它代替了ComObjActive("Excel.Application")因为后者会产生错误。 这里有一些有用的信息。 最后,请注意,我在Excel信任中心中启用了以下选项: Enable all macros (not recommended, potentially dangerous code can run)Trust access to the VBA project object model 。 此外,在COM加载项中的加载项部分没有被检查(我不知道是否重要)。 最后,我总是以pipe理员身份运行脚本。

当文件被closures时,这可以简单地使用VBAmacros来完成。

 FilePath = C:\Users\KostasK\Desktop\Plano.xlsx oExcel := ComObjGet(FilePath) oExcel.Worksheets("Sheet1").Range("C1").VALUE := "99" ; you can put actual sheet name instead "sheet1" oExcel.Save oExcel.Quit oExcel :=