Ask Your Question

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

asked 2017-04-27 00:19:07 +0200

EasyTrieve gravatar image

updated 2017-04-27 01:25:03 +0200

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.)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-10-02 11:57:36.883058

2 Answers

Sort by » oldest newest most voted

answered 2017-04-29 13:53:09 +0200

updated 2017-04-29 13:54:28 +0200

Check out Basic's On Error GoTo command.

The page Exception Handlingat 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."

edit flag offensive delete link more


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
   msgbox "er"
 End Sub
EasyTrieve gravatar imageEasyTrieve ( 2017-04-30 00:38:54 +0200 )edit

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
  msgbox "Error"
End Sub
techsquirrel gravatar imagetechsquirrel ( 2017-04-30 19:01:00 +0200 )edit

answered 2017-04-27 09:44:18 +0200

karolus gravatar image

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 )

edit flag offensive delete link more


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

karolus gravatar imagekarolus ( 2017-04-27 16:05:58 +0200 )edit

Thanks. Where do I find documentation to do this?

EasyTrieve gravatar imageEasyTrieve ( 2017-04-27 22:15:55 +0200 )edit
karolus gravatar imagekarolus ( 2017-04-27 22:54:30 +0200 )edit

@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.

EasyTrieve gravatar imageEasyTrieve ( 2017-04-28 02:54:42 +0200 )edit

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

librebel gravatar imagelibrebel ( 2017-04-29 14:15:33 +0200 )edit

@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.

EasyTrieve gravatar imageEasyTrieve ( 2017-04-30 00:43:28 +0200 )edit

Question Tools

1 follower


Asked: 2017-04-27 00:19:07 +0200

Seen: 1,194 times

Last updated: Apr 29 '17