Page 1 of 1

ComObj issue - Converting from AHK V1 to V2

Posted: 31 May 2023, 15:04
by Spitzi
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?

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

Posted: 31 May 2023, 21:07
by flyingDman
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

Re: ComObj issue - Converting from AHK V1 to V2

Posted: 01 Jun 2023, 02:13
by Spitzi
Thank you @flyingDman. It works nicely.