06 December, 2007

OpenOffice File Converter Macro

Some time back I published an entry showing how to execute a Open Office macro from the command line. This opens the possibilities of batch processing files, the original reason I started pursuing this. Authoring a series of publications stored in odt format allows storing in native file format. Unfortunately, since this file format is far from a conventional standard this is only useful if I can convert them to a more recognized standard (like pdf).

The macro defined below accepts a file name as an argument, converts it to an associated Pdf file and exits.


REM ***** BASIC *****

Sub SaveAsPDF( cFile )

' cFile = "/home/lipeltgm/sample.odt"
cURL = ConvertToURL( cFile )
oDoc = StarDesktop.loadComponentFromURL( ConvertToUrl( cFile ), "_blank", 0, Array())
cFile = Left( cFile, Len( cFile ) - 4 ) + ".pdf"
cURL = ConvertToURL( cFile )
oDoc.storeToURL( ConvertToUrl( cFile ), Array(MakePropertyValue( "FilterName", "writer_pdf_Export" )))
Shell("pkill soffice.bin")
End Sub

Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function



Execute the macro from the command line as follows:


$ soffice -invisible "macro:///Standard.Module1.SaveAsPDF(/home/myUser/sample.odt)"

It's worth noting that the 3rd '/' in 'macro:///' implies the root directory of the application, in this instance the root directory.

02 December, 2007

OpenOffice Command Line Macro Execution with Args

soffice "macro:///Standard.Module1.SaveAsPDF(/home/lipeltgm/sample.odt)"

macro:///
third / implies root directory of application

Command Line OpenOffice Macro Execution

First start by creating a new macro, something simple.

Create the macro by selecting Tools->Macros->Organize Macros->OpenOffice.org Basic

Edit a new Macro1 in Module1










Define the Macro1 as below:

Sub Macro1
MsgBox( "Hello World" )
End Sub


Save it and execute it from the command line by:

$ soffice macro:///Standard.Module1.Macro1

You can run OpenOffice headless by specifying the -invisible argument.

$ soffice -invisible macro:///Standard.Module1.Macro1