Running an OpenOffice Macro from the Java API

I am trying to write a Java program that will run an OpenOffice macro. I am getting this error:

java.lang.RuntimeException: com.sun.star.script.provider.ScriptFrameworkErrorException: Invalid format for Script URI: vnd.sun.star.script: Macro name

I believe it has something to do with what I call a macro (String cmd)

I searched for high and low but didn't seem to find any information on this. There are several posts on the OO forums, but none of them seem to have helped. Here are 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; 
}

      

}

+3


source to share


3 answers


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

As for the parameters, 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 this is helpful after such a long time ...: --(

+3


source


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

      



What is xComponent in the above code?

0


source


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

wring: "langauge": D, change "au" to "ua". :)

0


source







All Articles