2012-03-29 39 views
3

我正在嘗試編寫運行OpenOffice宏的Java程序。我得到這個錯誤:從Java API運行OpenOffice宏

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

我相信它有事情做與我打電話

我已經搜查高和低,但不能在宏(字符串CMD)的方式似乎在這方面找到任何信息。在OO論壇上有幾個帖子,但他們似乎都沒有幫助。下面是一些代碼:

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

宏類:

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

我想你的問題是在 「Name.Of.Macro」:它必須是:圖書館。 Module.NameOfMacro。 「langauge = Basic」當然會設置語言名稱,而「location = application」意味着應該在打開的文檔中搜索宏庫,而不是在全局OO庫中進行搜索。

至於參數都參與其中,我用:

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

希望它是有用的,這樣長的時間之後...... :-(

+0

自從我發佈這個問題已經很長時間了。我設法讓它在緊張情況下工作,但不記得我需要改變什麼。然而,既然你是唯一一個在133次觀看後迴應的人,我想給你一些道具:) – Anonymous 2012-06-14 13:11:49

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

什麼是xComponent在以上代碼?

+0

你能告訴我如何創建xComponent嗎? – 2013-01-09 00:38:23

+0

我做了:XComponent xComponent = xComponentLoader.loadComponentFromURL(「file:///」+ templateFile,「_blank」,0,loadProps); – MarcoS 2013-01-21 15:40:21

0

比較:?langauge =基本& location =文檔「 」:?language = Basic & location =文件「

wring是:」langauge「:D,將」au「換成」ua「。 :)