Invoke a function by its string name

Is it possible to call a function/sub by its string name in a macro ?

so if i have this function
sub xyz
end sub

any way to call it like “xyz” ??
i will have a list of functions in my module and they will be sequentially named like xyz1, xyz2, xyz3 etc
so if i have a variable ‘v’ that will have some number lets say 2 then i need to call the function xyz2
so i shd be able to call it like “xyz” & v
can a function be triggered in this way ?

I tried the vba version “application.ontime” but it has no effect

There is a solution, but YOU cannot use it, because its not BASIC!

You need invoke method.

Example. Put this text to document library “Standard”, module “Module1”. Call Test.

Option Explicit
Sub xyz
 Msgbox "I'm xyz"
End Sub

Sub Test
  Dim myMacro As String, script
  myMacro="Standard.Module1.xyz"    ' Library, Module, Macro
  script=ThisComponent.scriptProvider.getScript("vnd.sun.star.script:" & myMacro & "?language=Basic&location=document")
  script.invoke(Array(), Array(), Array())
End Sub

By changing the parameters, you can also call functions located in the application libraries and even written in other languages (for example, Python).

4 Likes

Turns out that it is even easier in Basic than in Python, which was a surprise for me.

thanks man good 1

@bloke, please mark the comment by @sokol32 as the solution.

And what do the 3 arrays do? The first array is passed as a full read-only argument list to the invoked macro; the other 2 are for out or in/out arguments: see documentation of XScript for details.


The link that was given in the original post points to the correct documentation (XScript, not XInvocation).

1 Like

Sorry, corrected, having trouble seeing everything on my phone. At least they both have the same interface, but I cannot see how they are related, if at all.

Use the Call Statement.

Sub xyz(val)
	MsgBox "xyz received " & val
End Sub

Sub DoXYZ
	v = "value"
	Call "xyz" v
End Sub
1 Like

After VBA, this would never even come to mind!
Unfortunately, the Call Statement does not allow a variable to be set as the first parameter.

1 Like

oh thats a pity

poor BASIC-hackers :yawning_face:

Eyes wide shut?

@jimk was on error in this specific case. That the Call command also accepts the name of the Sub to call if presented as a String constant, but (of course) not as the value of a String variable, actually is idiotic - and those who use Basic for specific purposes shouldn’t deny this. However, @sokol92 showed a well working and not too complicated way to do in Basic what was asked for.

I won’t study Python for my specific reasons, and I surely will never call StarBasic a powerful programming language/system.

But: There are reasons for many to use it in some cases, and it works on the level it is designed for .- and with a short bridge to the API.

1 Like

My answer uses a string literal. I couldn’t get string constants to work. The following code runs but does absolutely nothing.

s = "xyz"
Call CStr(s) (v)

Const subname = "xyz"
Call subname (v)

Sorry! This was what I meant. I used the wrong word. That Call "xyz" is interpreted exactly as if xyz was written is (imo) one of the very silly an misleading syntactical alternatives in Basic. VBA has lots of confusing ways of the kind, and some come from very old variants of Basic. I generally try to ignore them. (Concerning file operations there is a problem.)
Star (LibO) Basic simply doesn’t support procedural variables, but the API does (having its specific ways).

Anyway @sokol92 gave the correct answer, and LibreOffice: XInvocation Interface Reference is telling the rest.

To be precise, the object returned by the getScript method supports the Xscript interface and does not support the XInvocation interface.

You may try to use the yet undocumented CallByName() function …

Note: “CallByName” does not work when the returned value is an array

1 Like