ComObjActive("Excel.Application") won't work properly if there's more than one instance of Excel open
It's recommended to use
oExcel := Excel_Get() instead of
oExcel := ComObjActive("Excel.Application") to access the Excel object for a few reasons - when you use
oExcel := ComObjActive("Excel.Application");
• if there is more than 1 running Excel process, it will get the first registered on the ROT (Running Object Table).
• if the Excel process hasn't been registered on the ROT (if it was just opened), you will get a Com Error.
• you will get an error if the worksheet is being edited
oExcel := Excel_Get() ; access the Excel object(that's better way than oExcel := ComObjActive("Excel.Application"))
#Include Acc.ahk ; by Sean and Jethrow http://www.autohotkey.com/forum/viewtopic.php?t=82767
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
} ; http://www.autohotkey.com/forum/viewtopic.php?p=492448#492448