从Java API运行OpenOffice的宏 [英] Running OpenOffice Macro from Java API

查看:255
本文介绍了从Java API运行OpenOffice的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写将运行OpenOffice的宏的Java程序。我得到这个错误:

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:不正确
  vnd.sun.star.script:脚本URI形式的宏名称

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

我相信它有事情做与我打电话宏路(字符串CMD)

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

我已经搜查高和低,但似乎无法找到这方面的消息。上有OO论坛上几个职位,但他们都不似乎有助于。下面是一些code的:

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

宏类:

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

}

推荐答案

我想你的问题是在Name.Of.Macro:它必须是:Library.Module.NameOfMacro。
的langauge =基础课程的设置语言名称,以及位置=申请指宏观库应该打开的文档中进行搜索,而不是在全球OO库。

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... :-(

这篇关于从Java API运行OpenOffice的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆