Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Last active September 2, 2022 01:11
Show Gist options
  • Save JoeGlines/70e44e1d9012a6de67bf5e06e6c07dc5 to your computer and use it in GitHub Desktop.
Save JoeGlines/70e44e1d9012a6de67bf5e06e6c07dc5 to your computer and use it in GitHub Desktop.
;*******************************************************
; Want a clear path for learning AutoHotkey; Take a look at our AutoHotkey Udemy courses. They're structured in a way to make learning AHK EASY
; Right now you can get a coupon code here: https://the-Automator.com/Learn
;*******************************************************
XL:=XL_Handle(1)
first_row_of_data:=XL_First_Row(XL)+1 ;First row
MsgBox % "first row of data: " first_row_of_data
MsgBox % "last row: " XL_Last_Row(XL) ;Last row
MsgBox % "used rows: " XL_Used_Rows(XL) ;Used Rws
return
XL_First_Row(PXL){
Return, PXL.Application.ActiveSheet.UsedRange.Rows(1).Row ;first used row in Excel
}
XL_Last_Row(PXL){
Return, PXL.Application.ActiveSheet.UsedRange.Rows(PXL.Application.ActiveSheet.UsedRange.Rows.Count).Row
}
XL_Used_rows(PXL){
Return,PXL.Application.ActiveSheet.UsedRange.Rows.Count
}
; Function for connecting to Excel
XL_Handle(Sel){
ControlGet, hwnd, hwnd, , Excel71, ahk_class XLMAIN ;identify the hwnd for Excel
Obj:=ObjectFromWindow(hwnd,-16)
return (Sel=1?Obj.Application:Sel=2?Obj.Parent:Sel=3?Obj.ActiveSheet:"")
}
;***borrowd & tweaked from Acc.ahk Standard Library*** by Sean Updated by jethrow*****************
ObjectFromWindow(hWnd, idObject = -4){
if(h:=DllCall("LoadLibrary","Str","oleacc","Ptr"))
If DllCall("oleacc\AccessibleObjectFromWindow","Ptr",hWnd,"UInt",idObject&=0xFFFFFFFF,"Ptr",-VarSetCapacity(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0
Return ComObjEnwrap(9,pacc,1)
}
;***********************Show name of object handle is referencing********************************.
;~ XL_Reference(XL) ;will pop up with a message box showing what pointer is referencing
XL_Reference(PXL){
;~ MsgBox, %HWND%
;~ MsgBox, % ComObjType(window)
MsgBox % ComObjType(PXL,"Name")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment