I use this function instead of ComObjActive() for Word. All testing and feed back appreciated.
The code should look familiar
Word_Get.ahk (original version, below is cleaner)
Spoiler
Per opiuetasfd, removed a check for "0x80010001", this looks cleaner and works so far:
Code: Select all
; Word_Get.ahk: burque505, modified from
; Excel_Get by jethrow (modified)
; Forum: https://autohotkey.com/boards/viewtopic.php?f=6&t=31840
; Github: https://github.com/ahkon/MS-Office-COM-Basics/blob/master/Examples/Excel/Excel_Get.ahk
; With subsequent mods by opiuetasfd - thanks!
Word_Get(WinTitle:="ahk_class OpusApp", _WwG#:=1) {
static h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
WinGetClass, WinClass, %WinTitle%
if !(WinClass == "OpusApp")
return "Window class mismatch. (" WinClass ")"
ControlGet, hwnd, hwnd,, _WwG%_WwG#%, %WinTitle%
if (ErrorLevel)
return "Error accessing the control hWnd. (" ErrorLevel ")"
VarSetCapacity(IID_IDispatch, 16)
NumPut(0x46000000000000C0, NumPut(0x0000000000020400, IID_IDispatch, "Int64"), "Int64")
if (hr := DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", -16, "Ptr", &IID_IDispatch, "Ptr*", pacc)) != 0
return "Error calling AccessibleObjectFromWindow. ("
. (hr = 0x80070057 ? "E_INVALIDARG" : hr = 0x80004002 ? "E_NOINTERFACE" : hr) ")"
window := ComObject(9, pacc, 1)
if ComObjType(window) != 9
return "Error wrapping the window object."
try return window.Application
catch e
return "Error accessing the application object. (" SubStr(e.message, 1, 10) ")"
}
; References
; https://autohotkey.com/board/topic/88337-ahk-failure-with-excel-get/?p=560328
; https://autohotkey.com/board/topic/76162-excel-com-errors/?p=484371
; https://autohotkey.com/boards/viewtopic.php?p=134048#p134048
Spoiler
Word_GetExample2 (cloned from old Com example elsewhere in the forum, not sure just where right now. There is cruft left over from my SetTimer experiments)
Spoiler
All testing results greatly appreciated. So far it's working for me, but you never know. Thank you all for your time.Regards,
burque505