Page 1 of 1

Indirect macro invocation with object argument

Posted: Sat Jan 16, 2021 7:38 am
by DavidHMcCracken
I have registered a function to be invoked whenever any document is opened. Since only one macro can be assigned to this event, I want my function to play nicely by invoking macros specified by a user-defined list of similar functions in other libraries. Obviously, these functions cannot be embedded in mine and must, therefore, be invoked indirectly, i.e. by name as string. This "indirect" invocation is not difficult if no arguments or only string arguments are needed but the argument to this function is an object. Not passing the argument, the indirect invocation of each chained function is:
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, "macro:///" & LibModuleFunctionString, "", 0, Array())
This is a general question but I have provided the context to explain how this could be useful and also to invite completely different solutions to my immediate problem.

Re: Indirect macro invocation with object argument

Posted: Sat Jan 16, 2021 9:05 am
by Zizi64
PLease upload more samples (the code of the macros to call, and a sample document where you want to to call the macros from.)

You can call your own (user defined) macros by their name and by the passed parameters, not needed the Dispacther.
The Dispatcher is excellent (for example) to call the Calc Cell functions from a macro.

Code: Select all

sub My_main_routine
 
my_routine1("constant string parameter", sStringVariable)

floatResult = my_function2(floatNumberVariable1; floatNumberVariable2)

my_routine3(floatResult, floatNumberVariable3)

end sub
You can assingn the My_main_routine to the document event. It will call all of others routines and functions.
It can call the subs and functions from the MyMacros Standard library and/or from the Standard library of the actual document.

These libraries (the Standard ones) will be loaded into the memory, automatically. You must load-in the other libraries before you call a routine/function from them:

Code: Select all

...
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("MyLibrary1")) Then
		GlobalScope.BasicLibraries.LoadLibrary("MyLibrary1")
	End If
...

Re: Indirect macro invocation with object argument

Posted: Sat Jan 16, 2021 7:26 pm
by DavidHMcCracken
Thank you Zizi64 for your quick reply. Unfortunately, my situation is more complex than that. Indeed, my "on document open" macro does call several others, as you suggest, but they are all known at compile time and can be invoked directly. If the macro is not known until run time it must be invoked by reference. In C/C++ this is done with a pointer, in LISP with a delayed dereference operator. In BASIC I think it can only be done with a string passed to a mechanism that will interpret the string as a reference to a macro. Dispatcher is the only means that I have found. It does work but I can't find a way to pass an object argument to the macro. I'm hoping that someone either knows how to do that or can suggest an alternative means of invoking a macro that is not known until run time.

Re: Indirect macro invocation with object argument

Posted: Sat Jan 16, 2021 8:13 pm
by JohnSUN-Pensioner
Perhaps this solution will help you move on.

Re: Indirect macro invocation with object argument

Posted: Sat Jan 16, 2021 8:18 pm
by Villeroy
The stupid StarBasic language ignores any parameters as long as they remain unreferenced. Install the MRI extension and read [Tutorial] Introduction into object inspection with MRI
Bind your event to MyMacros>MriLib>Module1.Mri
This will show an object inspection window with all properties of the passed event struct, including the "Source" element which is the calling object.