OpenDocument dump macro

Here is a handy setup to automate dumping OpenDocument files to other formats. I’ve had to set this up on a few computers, and I tired of having to look up on the Internet how to do it each time.
Here is the command you can use for invocation of the macro. I have this shell script in my path named odtdump:
#!/bin/bash
if [ `echo "$1" | grep -c '^/'` -eq 0 ]; then
ooffice -invisible "macro:///Standard.MyConversions.SaveAsText(`pwd`/$1)"
else
ooffice -invisible "macro:///Standard.MyConversions.SaveAsText($1)"
fi
To set up the macro, open OpenOffice, go into the Macro Manager, click the Organizer button, and then create a new Macro under Standard. Call it MyConversions, and place the following text in the file:
' Based on code from http://www.oooforum.org/forum/viewtopic.phtml?t=3772
' Save document as an Acrobat PDF file.
Sub SaveAsPDF( cFile )
cURL = ConvertToURL( cFile )
' Open the document. Just blindly assume that the document
' is of a type that OOo will correctly recognize and open
' without specifying an import filter.
oDoc = StarDesktop.loadComponentFromURL( cURL, "blank", 0, _
Array(MakePropertyValue( "Hidden", True ),))
cFile = Left( cFile, Len( cFile ) - 4 ) + ".pdf"
cURL = ConvertToURL( cFile ) ' Save the document using a filter.
oDoc.storeToURL( cURL, Array(_
MakePropertyValue( "FilterName", "writerpdfExport" ),)
oDoc.close( True )
End Sub
' Save document as a Microsoft Word file.
Sub SaveAsDoc( cFile )
' mostly a copy of SaveAsPDF
cURL = ConvertToURL( cFile )
oDoc = StarDesktop.loadComponentFromURL( cURL, "blank", 0, (_
Array(MakePropertyValue( "Hidden", True ),))
cFile = Left( cFile, Len( cFile ) - 4 ) + ".doc"
cURL = ConvertToURL( cFile )
oDoc.storeToURL( cURL, Array(_
MakePropertyValue( "FilterName", "MS WinWord 6.0" ),)
oDoc.close( True )
End Sub
' Save document as a text file.
Sub SaveAsText( cFile )
' mostly a copy of SaveAsPDF
cURL = ConvertToURL( cFile )
oDoc = StarDesktop.loadComponentFromURL( cURL, "blank", 0, (_
Array(MakePropertyValue( "Hidden", True ),))
cFile = Left( cFile, Len( cFile ) - 4 ) + ".txt"
cURL = ConvertToURL( cFile )
oDoc.storeToURL( cURL, Array(_
MakePropertyValue( "FilterName", "Text" ),)
oDoc.close( True )
End Sub
' Save document as a csv file.
Sub SaveAsCSV( cFile )
' mostly a copy of SaveAsPDF
cURL = ConvertToURL( cFile )
oDoc = StarDesktop.loadComponentFromURL( cURL, "blank", 0, (_
Array(MakePropertyValue( "Hidden", True ),))
cFile = Left( cFile, Len( cFile ) - 4 ) + ".txt"
cURL = ConvertToURL( cFile )
oDoc.storeToURL( cURL, Array(_
MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ),)
oDoc.close( True )
End Sub
' Save document as an OpenOffice 2 file.
Sub SaveAsOOO( cFile )
' mostly a copy of SaveAsPDF. Save as an OpenOffice file.
cURL = ConvertToURL( cFile )
oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
Array(MakePropertyValue( "Hidden", True ),))
' Set output file extension based on lower-case
' version of input extension.
Select Case LCase(Right(cFile,3))
Case "ppt" ' PowerPoint file.
cFileExt = "odp"
Case "doc" ' Word file.
cFileExt = "odt"
Case "xls" ' Excel file.
cFileExt = "ods"
Case Else
cFileExt = "xxx"
End Select
cFile = Left( cFile, Len( cFile ) - 3 ) + cFileExt
cURL = ConvertToURL( cFile )
oDoc.storeAsURL( cURL, Array() )
oDoc.close( True )
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
Related Posts
Tags: macro • openoffice
Posted in solutions on November 7th, 2006 |

August 21st, 2007 at 11:21 am
[...] been very happy with the OpenOffice macro for dumping documents. However, recently I wanted to be able to dump Excel spreadsheets to tab-delimited text files, and [...]