Word_Get.ahk - use instead of ComObjActive

Post your working scripts, libraries and tools
burque505
Posts: 1353
Joined: 22 Jan 2017, 19:37

Word_Get.ahk - use instead of ComObjActive

18 Mar 2018, 12:30

Hello all, since my COM problems are not going away, I'm trying some workarounds.
I use this function instead of ComObjActive() for Word. All testing and feed back appreciated.
The code should look familiar :D

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
Word_GetExample1 (needs Word to be open or it will choke)
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
Last edited by burque505 on 18 Mar 2018, 16:52, edited 2 times in total.
opiuetasfd

Re: Word_Get.ahk - use instead of ComObjActive

18 Mar 2018, 14:41

Does Word have an "Edit Mode" like Excel? If not then the loop at the end is not required.
burque505 wrote:

Code: Select all

F7::  ; Press F7 to display Word's caption.
^I would just add that the variable should be cleared at the end: wdApp := "" The original example has this small flaw too.
burque505
Posts: 1353
Joined: 22 Jan 2017, 19:37

Re: Word_Get.ahk - use instead of ComObjActive

18 Mar 2018, 14:57

Thanks, opiutasfd.
^I would just add that the variable should be cleared at the end: wdApp := "" The original example has this small flaw too.
Updated OP.
Does Word have an "Edit Mode" like Excel? If not then the loop at the end is not required.
I tried taking the loop out, and it croaked.

I appreciate your help!
Regards,
burque505
opiuetasfd

Re: Word_Get.ahk - use instead of ComObjActive

18 Mar 2018, 15:25

I mean maybe you do not need to specifically check for the "0x80010001" error message anymore. not tested:

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
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
burque505
Posts: 1353
Joined: 22 Jan 2017, 19:37

Re: Word_Get.ahk - use instead of ComObjActive

18 Mar 2018, 16:54

opiuetasfd, your code works fine, OP updated. Thanks!
Original version is still there just in case. By the way, if you have any thoughts on modifying this for Outlook, I'd love to see your code.
Regards,
burque505
clina1j
Posts: 49
Joined: 22 Apr 2016, 18:39

Re: Word_Get.ahk - use instead of ComObjActive

22 Mar 2018, 13:28

Worked PERFECTLY for me. Allowed me to update an existing script, rather than start from scratch when a department asked me to automate the transfer of word documents from one template to another. MANY THANKS!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 15 guests