2012-03-29 16 views
3

Ich versuche, ein Java-Programm zu schreiben, das ein OpenOffice-Makro ausführen wird. Ich erhalte diese Fehlermeldung:Ausführen von OpenOffice Macro von Java API

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

Ich glaube, es hat etwas mit der Art und Weise zu tun hat, dass ich das Makro bin Aufruf (String cmd)

ich gesucht habe hoch und niedrig, kann aber nicht scheinen darüber irgendwelche Informationen zu finden. Es gibt ein paar Beiträge in den OO-Foren, aber keine davon schien zu helfen. Hier ist ein Teil des Codes:

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 Klasse:

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; 
} 

}

Antwort

3

Ich nehme an, Ihr Problem ist in der "Name.Of.Macro": es muss sein: Bibliothek. Modul.NameOfMakro. "langauge = Basic" legt natürlich den Namen der Sprache fest und "location = application" bedeutet, dass die Makrobibliothek im geöffneten Dokument und nicht in globalen OO-Bibliotheken gesucht werden soll.

Soweit Parameter beteiligt sind, die ich benutze:

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); 

Hoffe, dass es nützlich sein kann, nach so langer Zeit ... :-(

+0

Es ist schon eine Weile her, seit ich diese Frage gestellt habe. Ich schaffte es, es knirschend in Gang zu bringen, kann mich aber nicht erinnern, was ich ändern musste. Da du aber die EINZIGE Person bist, die nach 133 Zugriffen antwortet, möchte ich dir ein paar Requisiten geben :) – Anonymous

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

Was ist XComponent in ? der obige Code

+0

Könntest du mir bitte sagen, wie man xComponent erstellt? –

+0

Ich habe: XComponent xComponent = xComponentLoader.loadComponentFromURL ("Datei: ///" + templateFile, "_blank", 0, loadProps); – MarcoS

0

vergleichen: Langauge = Basis & location = Document“ auf:? language = Grund & location = Dokument "

wring ist:" langauge ": D, swap" au "zu" ua ". :)

Verwandte Themen