Dec 3, 2011

How to Convert Word to PDF Use Macro on OpenOffice

PDF is widely used to easily transfer documents via the Internet. PDF files are protected from changes and retain a presentation's exact elements, thereby avoiding problems like missing fonts or broken layouts. PDF files can be viewed with Adobe Acrobat Reader, an easily downloadable application that all OS-Windows, Linux, and Mac OS X support. This tutorial will guide you the free way of how to Use Macro on OpenOffice to Convert MS Word Documents to PDF on your Windows and Linux.


1. In OpenOffice, click Tools > Macros > Macro.
2. Click Organizer then choose the Libraries tab.
3. Set Application/Document to soffice, click New then name the library.
4. Click the Modules tab, select the library, then click New Module. Rename the module to Conversion for easier access.
5. When a window pops up, paste the following code:

Sub ConvertWordToPDF(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”, “writer_pdf_Export”), )) 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
6. Save and exit OpenOffice.

Use OpenOffice Macro on Linux to Convert MS Word Documents to PDF
Create a shell script in /usr/local/bin/:

#!/bin/sh DIR=$(pwd) DOC=$DIR/$1 /user/bin/oowriter-invisible “macro://Standard.Module1.ConvertWordToPDF($DOC)”

Run the shell script on the Microsoft Word document and it will create a PDF file.

Use OpenOffice Macro on Windows to Convert MS Word Documents to PDF
   After closing OpenOffice, create or open the file. Type this command:

@ECHO OFF “c:program filesOpenOffice.org1.1.0programsoffice”-invisible “macro:////NameofyourLibrary.Conversion.ConvertWordToPDF(%1)” Rem “c:program filesOpenOffice.org1.1.0programsoffice” -invisible “macro:////NameofyourLibrary.Conversion.test(%1)”

From the command line, type: convert c:docu9.doc. The docu9.pdf file can be found in the C: folder.

1 comment:

  1. Excellent tip, but please escape the code with code formatting - otherwise all the ‘ and “ and missing newlines interfere...

    ReplyDelete