ComObj issue - Converting from AHK V1 to V2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

ComObj issue - Converting from AHK V1 to V2

Post by Spitzi » 31 May 2023, 15:04

Hi there

In V1, i used this post viewtopic.php?t=114273 by @flyingDman to call a Word VBA function.

Specifically, the code i use is this:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.selection.wholestory
arange := oWord.Selection.Range
MsgBox, % aRange.text

type := (VT_BYREF := 0x4000) | (VT_I4 := 0x3)
VarSetCapacity(buff, 16, 0)
for k, v in ["x", "y", "w", "h"]
   %v% := ComObject(type, &buff + (k - 1)*4)
oWord.ActiveWindow.GetPoint(x, y, w, h, aRange)

for k, v in ["x", "y", "w", "h"]
   res .= (k = 1 ? "" : "`n") . v . ": " %v%[]
MsgBox, % res
It finds the coordinates on screen of a text displayed in word, in order to make a screenshot of it.

Now I am switching to V2 and I cannot get it to work... Buffers and ComValues are not my strongsuit. I tried stuff like

Code: Select all

		
		type := (VT_BYREF := 0x4000) | (VT_I4 := 0x3)
			buff := Buffer(16, 0)
			x := ComValue(type, &buff + (1 - 1)*4)
			y := ComValue(type, &buff + (2 - 1)*4)
			w := ComValue(type, &buff + (3 - 1)*4)
			h := ComValue(type, &buff + (4 - 1)*4)
insteat of the for loop, but I get errors that I do not understand.

How to convert the code into V2?

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: ComObj issue - Converting from AHK V1 to V2  Topic is solved

Post by flyingDman » 31 May 2023, 21:07

Try:

Code: Select all

#Requires AutoHotkey v2.0
oWord := ComObjActive("Word.Application")
oWord.selection.wholestory
arange := oWord.Selection.Range
MsgBox aRange.text

buff := Buffer(16, 0)
x := ComValue(0x4003, buff.ptr + (1 - 1)*4)
y := ComValue(0x4003, buff.ptr + (2 - 1)*4)
w := ComValue(0x4003, buff.ptr + (3 - 1)*4)
h := ComValue(0x4003, buff.ptr + (4 - 1)*4)

oWord.ActiveWindow.GetPoint(x, y, w, h, aRange)

for k, v in ["x", "y", "w", "h"]
   res .= (k = 1 ? "" : "`n") . v . ": " %v%[]
MsgBox res
14.3 & 1.3.7

Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

Re: ComObj issue - Converting from AHK V1 to V2

Post by Spitzi » 01 Jun 2023, 02:13

Thank you @flyingDman. It works nicely.

Post Reply

Return to “Ask for Help (v2)”