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

查看:23
本文介绍了从 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:不正确脚本 URI 的格式:vnd.sun.star.script:宏名称

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 论坛上有一些帖子,但似乎都没有帮助.部分代码如下:

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=Basic"当然是设置语言名称,location=application"表示宏库应该在打开的文档中搜索,而不是在全局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天全站免登陆