xscript invoke macro of specify file

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
orgoo
Posts: 71
Joined: Wed Apr 03, 2013 10:26 am

xscript invoke macro of specify file

Post 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!
LibreOffice 4.0.4.2 on Ubuntu 12.04
JulianR
Posts: 26
Joined: Mon Mar 12, 2018 9:41 am

Re: xscript invoke macro of specify file

Post 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
Apache OpenOffice 4.1.1 / LibreOffice 5.3 / LibreOffice 6.0 / LibreOffice 6.2 on Windows 7
Post Reply