我正在尝试从扩展程序中的java按钮运行宏(宏在Writer Standard.Voto.Main中)。
这是按钮:
<dlg:button dlg:id="Bt_voto" dlg:tab-index="2" dlg:left="78" dlg:top="15" dlg:width="52" dlg:height="12" dlg:tag="B" dlg:value="Voto"><script:eventscript:event-name="on-performaction" script:macro-name="vnd.sun.star.UNO:ConfigChange" script:language="UNO"/>
我从这个类中调用该方法:
public boolean callHandlerMethod(XDialog dialog, Object eventObject, String methodName)
throws WrappedTargetException {
if (methodName.equals(ConfigChange)) {
try {
String param = "";
// String cmd =
// "Standard.Conversion.ConvertHTMLToWord?langauge=Basic&location=application";
String cmd = "Standard.Voto.Main?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?");
onOkButtonPressed();
} catch (Exception e) {
System.out.println(e.toString());
// e.printStackTrace();
}
和Macro.class是:
class Macro {
public Object executeMacro(String strMacroName, Object[] aParams) throws BootstrapException
{
String oooExecFolder = "C:/Program Files (x86)/LibreOffice 5/program";
try {
System.out.println("Completed1");
XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExecFolder);
XMultiComponentFactory xMultiComponentFactory = xContext.getServiceManager();
XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", xContext));
System.out.println("Completed2");
com.sun.star.text.XTextDocument mxDoc = openWriter(xContext);
XScriptProviderSupplier xScriptPS = (XScriptProviderSupplier) UnoRuntime.queryInterface(XScriptProviderSupplier.class, xcomponentloader);
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];
aParams = new String[2];
@SuppressWarnings("unused")
Object result = xScript.invoke(aParams, aOutParamIndex, aOutParam);
System.out.println("xScript invoke macro " + strMacroName);
} catch (Exception e) {
throw new RuntimeException(e);
}
return oooExecFolder;
}
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;
}
}
如果我运行此类,则引导程序可以正常运行,但它终止于此行:
XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExecFolder);
没有错误。你能救我吗?
答案 0 :(得分:0)
直接从对话框中调用Basic宏要容易得多。即使扩展的其余部分使用Java,这也可以工作。请务必使用正确的Script URI。
<dlg:button dlg:id="Bt_voto" dlg:tab-index="3" dlg:left="173" dlg:top="88" dlg:width="51" dlg:height="13" dlg:value="Voto">
<script:event script:event-name="on-performaction" script:macro-name="vnd.sun.star.script:Standard.Voto.Main?language=Basic&location=application" script:language="Script"/>
</dlg:button>
但是,如果您确实需要通过Java来解决其他原因,请继续阅读。看起来您尝试组合来自两个不同项目的代码:
问题是某些命令只应该执行一次,每个项目都会执行,导致重复。不要第二次引导。相反,将上下文存储在某处,以便在需要时将其传递给Macro类。
以下是适用于我的代码的相关部分。为了测试,我向StarterProjectImpl.java添加了一个main方法。这样就可以看到错误消息。
public static void main (String [] args) {
com.sun.star.uno.XComponentContext xContext = null;
try {
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
ActionOneDialog actionOneDialog = new ActionOneDialog(xContext);
actionOneDialog.show();
} catch (Exception exc) {
exc.printStackTrace();
}
}
public ActionOneDialog(XComponentContext xContext) {
m_xContext = xContext;
this.dialog = DialogHelper.createDialog("ActionOneDialog.xdl", xContext, this);
}
public boolean callHandlerMethod(XDialog dialog, Object eventObject, String methodName) throws WrappedTargetException {
if (methodName.equals(ConfigChange)) {
try {
String cmd = "Standard.Voto.Main?language=Basic&location=application";
Macro macObj = new Macro(m_xContext);
}
}
public Object executeMacro(String strMacroName, Object[] aParams)
{
String oooExecFolder = "C:/Program Files (x86)/LibreOffice 5/program";
try {
XMultiComponentFactory xMultiComponentFactory = m_xContext.getServiceManager();
Object oDesktop = xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", m_xContext);
XComponentLoader xcomponentloader = (com.sun.star.frame.XComponentLoader)
UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
oDesktop);
com.sun.star.text.XTextDocument mxDoc = openWriter(m_xContext);
XScriptProviderSupplier xScriptPS = (XScriptProviderSupplier) UnoRuntime.queryInterface(
XScriptProviderSupplier.class, mxDoc);
XScriptProvider xScriptProvider = xScriptPS.getScriptProvider();
XScript xScript = xScriptProvider.getScript("vnd.sun.star.script:"+strMacroName);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
修改强>:
以下是分步说明。在LibreOffice中,转到Tools -> Macros -> Organize Macros -> LibreOffice Basic
。在My Macros & Dialogs -> Standard -> Voto
下,粘贴此代码:
Sub Main
MsgBox("Hello, Voto!")
End Sub
接下来是Get Started instructions。
展开dialog -> ActionOneDialog.xdl
并转到Source
标签。在</dlg:button>
之后粘贴以下代码。
<dlg:button dlg:id="Bt_voto" dlg:tab-index="4" dlg:left="100" dlg:top="88" dlg:width="51" dlg:height="13" dlg:value="Voto">
<script:event script:event-name="on-performaction" script:macro-name="vnd.sun.star.script:Standard.Voto.Main?language=Basic&location=application" script:language="Script"/>
</dlg:button>
现在运行它以添加扩展名。最后在LibreOffice中,转到Starter Project -> Action One
,然后点击Voto
。结果应该是这样的:
编辑2 :
根据我的经验,无法在对话框事件处理程序中插入字段。而是等到对话框完成执行后再插入字段。
对于此项目,它应该可以在ActionOneDialog.java中修改show()
。在命令dialog.execute()
之后,编写Java代码以使用调度程序插入字段。