R – Programmatically convert *.odt file to MS Word *.doc file using an OpenOffice.org basic macro

automationms-wordopenoffice-basicopenoffice.org

I am trying to build a reStructuredText to MS Word document tool-chain, so I will be able to save only the rst sources in version control.

So far I —

Have rst2odt.py to convert reStructuredText to OpenOffice.org Writer format.

Next I want to use the most recent OpenOffice.org (currently 3.1) that do a pretty decent work of generating a Word 97/2000/XP document, so I wrote the macro:

sub ConvertToWord(file as string)
  rem ----------------------------------------------------------------------
  rem define variables
  dim document   as object
  dim dispatcher as object
  rem ----------------------------------------------------------------------
  rem get access to the document
  document   = ThisComponent.CurrentController.Frame
  dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

  rem ----------------------------------------------------------------------
  dim odf(1) as new com.sun.star.beans.PropertyValue
  odf(0).Name = "URL"
  odf(0).Value = "file://" + file + ".odt"
  odf(1).Name = "FilterName"
  odf(1).Value = "MS Word 97"
  dispatcher.executeDispatch(document, ".uno:Open", "", 0, odf())

  rem ----------------------------------------------------------------------
  dim doc(1) as new com.sun.star.beans.PropertyValue
  doc(0).Name = "URL"
  doc(0).Value = "file://" + file + ".doc"
  doc(1).Name = "FilterName"
  doc(1).Value = "MS Word 97"

  dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, doc())
end sub

But when I executing it:

soffice "macro:///Standard.Module1.ConvertToWord(/path/to/odt_file_wo_ext)"

I get a: "BASIC runtime error. Property or method not found." message On the line:

document   = ThisComponent.CurrentController.Frame

And when I comment that line, the above invocation complete without error, but do nothing.
I guess I need to somehow set the value of document to a newly created instance, but I don't know how to do it.

Or am I going at it at a completely backward way?

P.S. I will consider JODConverter as a fallback, because I try to minimize my dependencies.

Best Answer

I would suggest using JODConverter (your fallback) because you get what you want and when/if OpenOffice/LibreOffice make improvements to their DOC filters, you don't have to install/upgrade/test your macro each time. It's pretty well proven too.

Related Topic