BASIC: How to test if a sub or function exists?

In BASIC is there a way to test if a given named sub or function exists, i.e. is defined?

If I use ThisComponent.BasicLibraries.getByName("Standard").getByName("Module1") I can get the BASIC code that I’m interested in, and then I can search it with instr, but I was hoping for a way to have LO parse the BASIC and tell me if a given, named module exists.

Similar to this in VBA.

Would also appreciate it if you could tell me how to call ANY given module (Sub or Function), regardless of default scoping.

(I want code in My Macros & Dialogs to see if my current document has a function to provide modifiers to the more general code. In other words I want my document to call a program in the global library, that intern looks for and possibly calls code in my document’s basic.)

In other words I want my document to call a program in the global library, that intern looks for and possibly calls code in my document’s basic.

from inside doc:

  …
 global_sub( other_sub )
 …

and ‘global_sub’ should look like:

global_sub( some_procedur )

     some_procedur

obviously not possible with old crumpy basic, maybe another chance for drop that shit and work with more advanced languages like python.

Thanks. Where do I find documentation to do this?

Hallo

python-docs

@Karolus, Sorry, I meant where do I find docs on how to use LO w/ Phthon? Lack of docs on this interface is what has kept me from looking at languages other than basic.

Hello @EasyTrieve,
writing LibreOffice scripts in Python: Python as a macro language
PyUNO samples: PyUNO samples - Apache OpenOffice Wiki
( btw i think that your original idea of using instr() would be the simplest solution here)

@Karolus, Thanks. I’ll check it out. As to instr(), lazy parsing has its downsides, as it can later be fooled and thus is unreliable. Yes, simple, but error prone. I was looking for a robust way to tell if the procedure existed or not, something like isObject, or the like. I have been spoiled by php I think. BTW, Access, at least the Access that I have also didn’t have a very good way to answer this question, but there was a procedure that did work, if you could find it.

Check out Basic’s On Error GoTo command.

The page Exception Handling at Exception Handling - Apache OpenOffice Wiki discusses a situation similar to yours. It explains that “Unlike UNO, Basic does not support exceptions. All exceptions thrown by UNO are caught by the Basic runtime system and transformed to a Basic error.” and “Exceptions transformed to Basic errors can be handled just like any Basic error using the On Error GoTo command.”

Good idea, as I had already tried this. If you want to see for yourself that this doesn’t work just patch this in and run it. It can’t find Y, and the error trap never runs.

 Sub X
   On Error GoTo Z
   Y
 Z:
   msgbox "er"
 End Sub

You’re right… As far as I can tell, you can’t check for a sub or function that way; you’d have to parse the BASIC code. You can, however, use On Error Goto to check for module existence. The following shows “Ok!” if Module1 exists. Change Module1 to a nonexistant module, and it shows “Error”.

Sub X
	On Error GoTo Z
   	BasicCode = GlobalScope.BasicLibraries.getByName("Standard").getByName("Module1")
   	msgbox "Ok!"
    Exit Sub
Z:
  msgbox "Error"
End Sub