Convert OpenOffice to Microsoft Office

My first exposure to OpenOffice was a demo from a Sun representative where he was extolling the virtues of Sun’s StarOffice while running his actual presentation from another product (Microsoft PowerPoint). It clearly wasn’t ready for general consumption at that point, but it quickly improved and became the predominant office platform outside of Microsoft Office.

Unfortunately, Oracle acquired Sun and has since discontinued the project and will return the OpenOffice framework to the developer community. There are a few new projects that have cropped up to address the gaps Oracle OpenOffice may be introducing, including the popular LibreOffice, but in the near term it’s not really clear what the prospects for OpenOffice.org are going to be.

So, despite my better judgment, after more than 10 years with OpenOffice I’m planning to standardize on Microsoft Office for a while. It has only been in the last few years that I have moved from a Linux desktop to Mac, which makes this transition even easier. However, I have a significant number of documents in OpenOffice format that need to get converted into MS Word, Excel and PowerPoint. Fortunately, I can use OpenOffice to do this for me.

My first step is to create some macros for automating the conversion, you can find this under Tools – Macros – Organize Macros – OpenOffice.org Basic.

Here you can see the macros that I have added under Module1, you may choose to create a new module or simple amend as I have done here. For each core document type, I have added a macro that is able to convert that file.

' Save document as a Microsoft Word file.
Sub SaveAsDoc( cFile )
   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 Microsoft Excel file.
Sub SaveAsXls( cFile )
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, (_
            Array(MakePropertyValue( "Hidden", True ),))

   cFile = Left( cFile, Len( cFile ) - 4 ) + ".xls"
   cURL = ConvertToURL( cFile )

   oDoc.storeToURL( cURL, Array(_
            MakePropertyValue( "FilterName", "MS Excel 97" ),)
   oDoc.close( True )

End Sub

' Save document as a Microsoft PowerPoint file.
Sub SaveAsPpt( cFile )
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, (_
            Array(MakePropertyValue( "Hidden", True ),))

   cFile = Left( cFile, Len( cFile ) - 4 ) + ".ppt"
   cURL = ConvertToURL( cFile )

   oDoc.storeToURL( cURL, Array(_
            MakePropertyValue( "FilterName", "MS PowerPoint 97" ),)
   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

The next trick is to automate this, and I did that with a quick shell script. It’s really nothing fancy, and could easily be improved on – the idea is to find all the corresponding document types (ODT / ODS / ODP) and then execute the right macro from OpenOffice to convert from the OOO format into an MS Office file:

#!/bin/sh
export PATH=$PATH:/Applications/OpenOffice.org.app/Contents/MacOS

find ~/Documents -name \*.odt -print -exec soffice -invisible "macro:///Standard.Module1.SaveAsDoc({})" \;
find ~/Documents -name \*.ods -print -exec soffice -invisible "macro:///Standard.Module1.SaveAsXls({})" \;
find ~/Documents -name \*.odp -print -exec soffice -invisible "macro:///Standard.Module1.SaveAsPpt({})" \;

That’s it. With a final backup from TimeMachine, I then went ahead and removed the remaining OpenOffice documents from my Document directory.