[Solved] Call Calc doc. Basic Sub from getScript impossible?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
gurfle
Posts: 67
Joined: Wed Dec 02, 2009 5:40 am

[Solved] Call Calc doc. Basic Sub from getScript impossible?

Post by gurfle »

I have a Sub MakeNormCoinToss in a module called "RandomCode" in a Calc document called "Annex.ods", and this call to getScript

Code: Select all

getScript("vnd.sun.star.script:Annex.RandomCode.MakeNormCoinToss?language=Basic&location=document")
fails with the error

Code: Select all

The following Basic script could not be found:
library: 'Annex'
module: 'RandomCode'
method: 'MakeNormCoinToss'
location: 'document'
If, however, I move the Sub MakeNormCoinToss to the Tools module in the "My Macros" Standard Library I can get the same getscript call to complete successfully - of course after replacing "Annex.RandomCode" with "Standard.Tools" and "location=document" with "location=application".

Is it not possible to use getscript to start up a Basic Sub embedded in a document?
Last edited by gurfle on Tue Feb 23, 2010 5:23 am, edited 1 time in total.
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard

AntiX M11
LibreOffice 3.4.3

Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
User avatar
Villeroy
Volunteer
Posts: 31349
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Call of Basic Sub in Calc doc. from getScript impossible ??

Post by Villeroy »

Run this first:

Code: Select all

someDoc.BasicLibraries.loadLibrary("Annex")
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
gurfle
Posts: 67
Joined: Wed Dec 02, 2009 5:40 am

Re: Call of Basic Sub in Calc doc. from getScript impossible ??

Post by gurfle »

Thanks, Villeroy, for the suggestion :). Unfortunately it did not help. :(

Even though the code making the call to MakeNormCoinToss also resides in the same "Annex" document (in fact in the same "RandomCode" module), I really want to use getScript - instead of a straight Basic sub call - because the calling code needs to build the name of the sub to be called from string variables.

When I check (from Annex.ods) thiscomponent.BasicLibraries.ElementNames, "Annex" and "Standard" are listed.

I tried moving MakeNormCoinToss to a module in the "Standard" library of Annex.ods with no success either. Only by moving MakeNormCoinToss to the "My Macros" Standard.Tools module can I get it to run from getScript.

At least it sounds from your reply that what I am attempting ought to be possible, but I am out of ideas as to what I am doing wrong or where to search. As I said, I am trying everything from just one document.

Thanks, Nick
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard

AntiX M11
LibreOffice 3.4.3

Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Call of Basic Sub in Calc doc. from getScript impossible ??

Post by B Marcelly »

Hi,
You did not show your not-working code :evil:

You must get the script provider from the document, not from the MasterScriptProviderFactory.
This is a working call (in Basic).

Code: Select all

Dim scriptPro As Object, myScript As Object

scriptPro = ThisComponent.getScriptProvider()
myScript = scriptPro.getScript("vnd.sun.star.script:Library1.Module1.Main?language=Basic&location=document")
myScript.invoke(Array(), Array(), Array() )
You don't need to load the library before calling the script.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
gurfle
Posts: 67
Joined: Wed Dec 02, 2009 5:40 am

Re: Call of Basic Sub in Calc doc. from getScript impossible ??

Post by gurfle »

B Marcelly wrote:Hi,
You did not show your not-working code :evil:

You must get the script provider from the document, not from the MasterScriptProviderFactory.
Indeed, you figured out what was wrong without even seeing my code :super:
I had been extracting what seemed relevant pieces from the example at http://www.oooforum.org/forum/viewtopic.phtml?t=59534:

Code: Select all

createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory").createScriptProvider("").getScript("vnd.sun.star.script:Annex.RandomCode.MakeNormCoinToss?language=Basic&location=document").invoke(arglist,Array(),Array())
but your correction not only works, but is a lot less ugly ;) :

Code: Select all

ThisComponent.getScriptProvider().getScript("vnd.sun.star.script:Annex.RandomCode.MakeNormCoinToss?language=Basic&location=document").invoke(arglist,Array(),Array())
Not quite sure yet about the subtle distinction between these alternatives, but having such interesting examples to ponder now should help me get it soon enough.

In the future I commit to making more effort at extricating key pieces of code to illustrate such hangups. Again, :bravo: :bravo: to B Marcelly for seeing that the document script provider as opposed to the MasterScriptProviderFactory was the culprit.
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard

AntiX M11
LibreOffice 3.4.3

Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
Post Reply