Dynamic COM call from Text?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Heezea
Posts: 59
Joined: 30 Sep 2013, 21:33

Dynamic COM call from Text?

06 Sep 2022, 12:41

Is it possible to perform a dynamic COM call from text? Attempted example below.

Code: Select all

Esc::ExitApp
q::
	MsgBox, % fTest("xl.Selection.Address")
	return
fTest(pCmd){
	xl:=ComObjActive("Excel.Application")
	MsgBox, % xl.Selection.Address ;Correct
	MsgBox, % pCmd ;Returns the correct text.
	%pCmd%() ;Doesn't work
	;~%pCmd% ;Doesn't work
}
Also tried with AHKv2 but couldn't get it to work either.
Spoiler
[Mod edit: Changed to code tags, since original codebox tags didn't use AHK formatting]
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: Dynamic COM call from Text?

06 Sep 2022, 15:09

What do you want %pCmd% to do?
14.3 & 1.3.7
Heezea
Posts: 59
Joined: 30 Sep 2013, 21:33

Re: Dynamic COM call from Text?

07 Sep 2022, 05:46

@flyingDman, I'd like it to run the code xl.Selection.Address, which should return something like $A$1. The first MsgBox (with the comment "Correct" in the code) actually runs the command, for comparison. This is just an example. If I sent some other command, I'd like that to execute.

I know there's alternate ways, like parsing the code or using conditionals to check the text and then hardcode the commands but the most convenient would be to be able to execute the text dynamically.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: Dynamic COM call from Text?

07 Sep 2022, 07:25

Xl.Selection.Address by itself would not do anything. So a variable containing it would not do anything either. It resolves - there is no error - but stays in memory. Adding () makes it more confusing.
14.3 & 1.3.7
Heezea
Posts: 59
Joined: 30 Sep 2013, 21:33

Re: Dynamic COM call from Text?

07 Sep 2022, 08:49

Yes I see what you mean. I guess my example is not good but I'd hoped the intent would be clear. Here's a new example.

I had added the () in an attempt to make it run though I do understand that's how you dynamically call a function.

Code: Select all

q::
{	
	xl:=ComObjActive("Excel.Application")
	str := "xl.Selection.Address"
	MsgBox, % "1." xl.Selection.Address ;Control test - expected result = $A$1
	MsgBox, % "2." str ;Shows xl.Selection.Address, but not the expected result.
	MsgBox, % "3." %str% ;Doesn't work at all, throws an error.
	MsgBox, % "4." %str%() ;Returns blank; assume calling a method of a class that doesn't exist or maybe it does exist but it's dynamically created and essentially blank.
	MsgBox, 5. %str% ;Same as 2.
	MsgBox, 6. %str%() ;Same as 2 but with ().
	return
}
Esc::ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 200 guests