Ask Your Question
0

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

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 close merge delete

2 Answers

Sort by » oldest newest most voted
1

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 https://wiki.openoffice.org/wiki/Docu... 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

Comments

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

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 )

     some_procedur
edit flag offensive delete link more

Comments

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: https://wiki.openoffice.org/wiki/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
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 669 times

Last updated: Apr 29 '17