excel - AutoHotkey's ComObjActive handle to specific Worksheet -


the simplest way create handle active excel sheets in .ahk script is:

xl := comobjactive("excel.application") 

nonetheless, if 1 switches workbook, becomes new "currently active sheet" , autohotkey tries use methods sheets , cells on new workbook through com: of course scripts designed work specific sheets , cells don't work anymore on different workbook.

do know how create com handles specific workbooks instead of active sheet?

the goal should allow user loop between workbooks without xl object losing previous handle , going new one.

example

open excel workbook scratch , type 1234 in cell a1 of sheet named "sheet1"; create new .ahk script following content:

#persistent  xl := comobjactive("excel.application") settimer, xlread, 5000 return  xlread: {   value := xl.sheets("sheets1").range("a1").value   msgbox, %value% } return 

script above should display "1234" in message box every 5 seconds.

while running, open new workbook , type 5678 in cell a1 of sheet named "sheet1" , wait 5 seconds: according trials, autohotkey should switch handle new active sheet , show message box content "5678".

any way keep linked first sheet? of course assume 1 can save excel files hard disk proper names com can refer to.

one way store active workbook object in variable this

#persistent  oexcel := comobjactive("excel.application") this_book := oexcel.activeworkbook settimer, xlread, 10000 return  xlread: {   value := this_book.sheets(1).range("a1").value   msgbox, %value% } return 

2nd way use comobjget full name of active workbook if know before hand no need controlgettext command use workbooks full name

#persistent settitlematchmode, 2  controlgettext, workbookname, excel71, microsoft excel oworkbook := comobjget(workbookname)  settimer, xlread, 10000 return  xlread: {   value := oworkbook.sheets(1).range("a1").value   msgbox, %value% } return 

you can use comobjget full path of excel file return workbook object

#persistent fileselectfile, path  oworkbook := comobjget(path) settimer, xlread, 10000 return  xlread: {   value := oworkbook.sheets(1).range("a1").value   msgbox, %value% } return 

hope helps need


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -