Anonymous
Anonymous

Reputation: 1979

Running OpenOffice Macro from Java API

I'm trying to write a Java program that will run an OpenOffice Macro. I'm getting this error:

java.lang.RuntimeException: com.sun.star.script.provider.ScriptFrameworkErrorException: Incorrect format for Script URI: vnd.sun.star.script:Name of macro

I believe it has something to do with the way that I'm calling the macro (String cmd)

I've searched high and low but can't seem to find any information on this. There are a few posts on the OO forums but none of them seemed to help. Here is some of the code:

    public static void main(String[] args) throws BootstrapException {
   if(args.length == 0)
   {
       System.out.println("Must enter a filename");
       System.exit(1);
   }
   try
   {

        String param = args[0];
        //String cmd = "Standard.Conversion.ConvertHTMLToWord?langauge=Basic&location=application";
        String cmd = "Name.Of.Macro?langauge=Basic&location=Document";
        System.out.println("Running macro on " + param);
        Macro macObj = new Macro();
        macObj.executeMacro(cmd, new Object[]{param}]);
        System.out.println("Completed");
   }
   catch(Exception e)
   {
       System.out.println(e.toString());
       //e.printStackTrace();
   }

Macro Class:

class Macro {
private static final String ooExecPath = "C:/Program Files/OpenOffice.org 3/program";
public Object executeMacro(String strMacroName, Object[] aParams) throws BootstrapException
{
    try
    {
        com.sun.star.uno.XComponentContext xContext;

        System.out.println("Connecting to OpenOffice");
        xContext = BootstrapSocketConnector.bootstrap(ooExecPath);
        System.out.println("Connected to a running instance of OpenOffice");
        System.out.println("Trying to execute macro...");

        com.sun.star.text.XTextDocument mxDoc = openWriter(xContext);

        XScriptProviderSupplier xScriptPS = (XScriptProviderSupplier) UnoRuntime.queryInterface(XScriptProviderSupplier.class, mxDoc);
        XScriptProvider xScriptProvider = xScriptPS.getScriptProvider(); 
        XScript xScript = xScriptProvider.getScript("vnd.sun.star.script:"+strMacroName); 

        short[][] aOutParamIndex = new short[1][1]; 
        Object[][] aOutParam = new Object[1][1];


        return xScript.invoke(aParams, aOutParamIndex, aOutParam); 

    } catch (Exception e) { 
        throw new RuntimeException(e); 
    } 
}

public static com.sun.star.text.XTextDocument openWriter(com.sun.star.uno.XComponentContext xContext)
{

    com.sun.star.frame.XComponentLoader xCLoader; 
    com.sun.star.text.XTextDocument xDoc = null; 
    com.sun.star.lang.XComponent xComp = null; 

    try { 
        // get the remote office service manager 
        com.sun.star.lang.XMultiComponentFactory xMCF = 
            xContext.getServiceManager(); 

        Object oDesktop = xMCF.createInstanceWithContext( 
                                    "com.sun.star.frame.Desktop", xContext); 

        xCLoader = (com.sun.star.frame.XComponentLoader) 
            UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class, 
                                      oDesktop); 
        com.sun.star.beans.PropertyValue [] szEmptyArgs = 
            new com.sun.star.beans.PropertyValue [0];
       /* 
        ArrayList<PropertyValue> props = new ArrayList<PropertyValue>();
        PropertyValue p = new PropertyValue();
        p.Name = "Hidden";
        p.Value = new Boolean(true);
        props.add(p);

        PropertyValue[] properties = new PropertyValue[props.size()];
        props.toArray(properties);
        String strDoc = "private:factory/swriter";
        xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, properties);            
        */
        String strDoc = "private:factory/swriter"; 
        xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs); 
        xDoc = (com.sun.star.text.XTextDocument) 
            UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, 
                                      xComp); 

    } catch(Exception e){  
        System.err.println(" Exception " + e); 
        e.printStackTrace(System.err); 
    }        
    return xDoc; 
}

}

Upvotes: 3

Views: 3054

Answers (3)

23nikoloz
23nikoloz

Reputation: 70

Compare: ?langauge=Basic&location=Document" to: ?language=Basic&location=Document"

wring is: "langauge" :D, swap "au" to "ua". :)

Upvotes: 0

Naveen A
Naveen A

Reputation: 553

XScriptProviderSupplier xScriptPS = (XScriptProviderSupplier) UnoRuntime.queryInterface(XScriptProviderSupplier.class, xComponent);

What is xComponent in the above code?

Upvotes: 0

MarcoS
MarcoS

Reputation: 17721

I suppose your problem is in the "Name.Of.Macro": it must be: Library.Module.NameOfMacro. "langauge=Basic" of course sets the language name, and "location=application" means the macro library should be searched in the opened document, and not in global OO libraries.

As far as parameters are involved, I use:

XScriptProviderSupplier xScriptPS = (XScriptProviderSupplier) UnoRuntime.queryInterface(XScriptProviderSupplier.class, xComponent);
XScriptProvider xScriptProvider = xScriptPS.getScriptProvider(); 
XScript xScript = xScriptProvider.getScript("vnd.sun.star.script:"+macroName); 

short[][] aOutParamIndex = new short[1][1];
Object[][] aOutParam = new Object[1][1];

Object[] aParams = new String[2];
aParams[0] = myFirstParameterName;
aParams[1] = mySecondParameterName;
@SuppressWarnings("unused")
Object result = xScript.invoke(aParams, aOutParamIndex, aOutParam);
System.out.println("xScript invoke macro " + macroName);

Hope it can be useful, after such long time... :-(

Upvotes: 3

Related Questions