Search code examples
openoffice.orglibreofficemailmergeoffice-automation

Open a libreoffice mail merged textdocument directly with swriter


I need help with opening the result of my mail merge operations directly in an new writer document.

    Object mailMergeService = mcf.createInstanceWithContext(mailMergePackage, context);
    XPropertySet mmProperties = UnoRuntime.queryInterface(XPropertySet.class, mailMergeService);
    mmProperties.setPropertyValue("DocumentURL", templatePath);
    mmProperties.setPropertyValue("DataSourceName", dbName);
    mmProperties.setPropertyValue("CommandType", mmCommandType);
    mmProperties.setPropertyValue("Command", mmCommand);
    mmProperties.setPropertyValue("OutputType", mmOutputType);
    // mmProperties.setPropertyValue("OutputURL", templateDirectory);
    // mmProperties.setPropertyValue("FileNamePrefix", mmFileNamePrefix);
    // mmProperties.setPropertyValue("SaveAsSingleFile", mmSaveAsSingleFile);

The mmOutputType is set as MailMergeType.SHELL The LibreOffice API documentation says

"The output is a document shell. The successful mail marge returns a XTextDocument based component."

So I've tried something like this

    XJob job = UnoRuntime.queryInterface(XJob.class, mailMergeService);
    Object mergedTextObject = job.execute(new NamedValue[0]);

    String url = "private:factory/swriter";
    loader.loadComponentFromURL(url, "_blank", 0, new PropertyValue[0]);

    XTextDocument mergedText = UnoRuntime.queryInterface(XTextDocument.class, mergedTextObject);
    XTextCursor cursor = mergedText.getText().createTextCursor();
    cursor.setString(mergedText.getText().getString());

I guess I have to pass the XTextDocument component to the url-argument of the loadComponentFromURL method but I didnt find the right way to do that.

When I change the OutputType to MailMergeType.FILE the result is generated in a given directory and I can open the file and see that the mail merge succeeded. But this is not what my application should do.

Does someone know how I can open the result of the mail merge directly in an new writer document without saving the result to the hard drive?

Sincerly arthur


Solution

  • Hey guys I've found a simple way to open the result of my mail merge process directly.

    The relevant snippets are these

      XJob job = UnoRuntime.queryInterface(XJob.class, mailMergeService);
      Object mergedTextObject = job.execute(new NamedValue[0]);
      XTextDocument mergedText = UnoRuntime.queryInterface(XTextDocument.class, mergedTextObject);
      mergedText.getCurrentController().getFrame().getContainerWindow().setVisible(true);
    

    The last line of code made the window appear with the filled mail merge result.

    I also don't need this line anymore

    loader.loadComponentFromURL("private:factory/swriter", "_blank", 0, new PropertyValue[0]);
    

    The document opens as a new instance of a swriter document. If you want to save the result as a file you can do this

      mergedText.getCurrentController().getFrame().getContainerWindow().setVisible(true);
      XStorable storeMM = UnoRuntime.queryInterface(XStorable.class, mergedText);
      XModel modelMM = UnoRuntime.queryInterface(XModel.class, mergedText);
      storeMM.storeAsURL(outputDirectory + outputFilename, modelMM.getArgs());
    

    Sincerly Arthur