Page 1 of 1

xscript invoke macro of specify file

Posted: Tue May 07, 2013 1:46 pm
by orgoo
Hi All,

I'm using next code to invoke macro function in java class :

Code: Select all

     XComponentContext mxRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
    XMultiComponentFactory mxRemoteServiceManager = mxRemoteContext	.getServiceManager();
    
    Object oMSPFac = mxRemoteServiceManager.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory",mxRemoteContext);
    XScriptProviderFactory xScriptProviderFactory = (XScriptProviderFactory) UnoRuntime.queryInterface(XScriptProviderFactory.class, oMSPFac);

    Object oMSP = xScriptProviderFactory.createScriptProvider("");
    XScriptProvider xScriptProvider = (XScriptProvider) UnoRuntime.queryInterface(XScriptProvider.class, oMSP);

    XScript xScript = xScriptProvider.getScript("vnd.sun.star.script:Standard.Module1.fnTest?language=Basic&location=application");

   short[][] aOutParamIndex = new short[1][1];
   Object[][] aOutParam = new Object[1][1];
			
   String[] test= {"1","2","3"};   
			
   xScript.invoke(test, aOutParamIndex, aOutParam);
 
this code can call the macro correctly, but the macro must be placed in libreoffice installation, e.g. C:\Users\username\AppData\Roaming\LibreOffice\4\user\basic.

how can I call the macro function of specify file? e.g. I want to call one macro function of E:\temp.ods.

Thanks in advance!

Re: xscript invoke macro of specify file

Posted: Tue Apr 28, 2020 12:40 pm
by JulianR
If anyone is looking for an answer to such question, then the answer is:
The line containing:

Code: Select all

Object oMSP = xScriptProviderFactory.createScriptProvider("");
xScriptProviderFactory.createScriptProvider("") instead of empty string must be provided with object instance of class XComponent, that holds the reference to the document containing the macro. (for example created with XComponentLoader)
And the line containing

Code: Select all

XScript xScript = xScriptProvider.getScript("vnd.sun.star.script:Standard.Module1.fnTest?language=Basic&location=application");
last parameter - location should be changed from application to document