2012-03-29 3 views
3

J'essaie d'écrire un programme Java qui exécutera une macro OpenOffice. Je reçois cette erreur:Exécution de la macro OpenOffice à partir de l'API Java

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

Je crois qu'il a quelque chose à voir avec la façon dont j'appelle la macro (String cmd)

J'ai recherché haut et bas mais ne peut pas semblent trouver des informations à ce sujet. Il y a quelques messages sur les forums OO mais aucun d'eux n'a semblé aider. Voici le 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(); 
    } 

Classe Macro:

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

}

Répondre

3

Je suppose que votre problème est dans le "Name.Of.Macro": il doit être: Bibliothèque. Module.NameOfMacro. "langauge = Basic" définit bien sûr le nom de la langue, et "location = application" signifie que la bibliothèque de macros doit être recherchée dans le document ouvert, et non dans les bibliothèques OO globales.

En ce qui concerne les paramètres sont impliqués, j'utilise:

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

L'espoir il peut être utile, après si longtemps ... :-(

+0

Cela fait un certain temps que j'ai posté cette question. J'ai réussi à le faire fonctionner dans un crunch, mais je ne me souviens pas de ce que j'avais besoin de changer. Cependant, puisque vous êtes la seule personne à répondre après 133 vues, je voudrais vous donner quelques accessoires :) – Anonymous

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

Qu'est-ce que XComponent dans le code ci-dessus?

+0

Pourriez-vous me dire comment créer xComponent? –

+0

J'ai fait: XComponent xComponent = xComponentLoader.loadComponentFromURL ("fichier: ///" + templateFile, "_blank", 0, loadProps); – MarcoS

0

Comparer:? langauge = De base & location = Document " to:? language = Basic & location = Document "

L'essorage est:" langauge ": D, échange" au "vers" ua ". :)

Questions connexes