Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts. A copy of the license is included in the section entitled "GNU
Free Documentation License".

Code samples in this article are hereby put in the public domain.

Disclaimer

The author disclaims all warranties with regard to this document,
including all implied warranties of merchantability and fitness for a
certain purpose; in no event shall the author be liable for any
special, indirect or consequential damages or any damages whatsoever
resulting from loss of use, data or profits, whether in an action of
contract, negligence or other tortious action, arising out of or in
connection with the use of this document.

Formatting documents with OpenOffice.org Writer macros

Around the time OpenOffice.org 1.1 RC was released, I was migrating a small company from Corel WordPerfect to OpenOffice.org. OpenOffice.org by itself does not support reading or writing WordPerfect files, but a tool called wpd2sxw can convert WordPerfect files to OpenOffice.org format (SXW). After conversion with wpd2sxw, which was rather good but had problems with some formatting features, I applied macros to documents based on different templates to make more than 2,000 converted documents look very similar to original WordPerfect files they were generated from. This article presents some macro "building blocks" you can use to modify a document's formatting or to generate well-formatted documents from plain text files.

OpenOffice.org uses StarBasic as its macro language. I won't go into details of the language itself, but the examples given here should be easy to understand if you have some programming experience. You can find tutorials and general information regarding StarBasic macros, along with information on OpenOffice.org scripting, elsewhere on the Web.

You can edit and run macros in OpenOffice.org through the Macro dialog box (Tools->Macros->Macro...), which is more or less self-explanatory. For brevity, the macros listed in this article usually don't declare variables they use, so they won't work with Option Explicit. Generally, they were designed for OpenOffice.org 1.1, but most also work in 1.0 and should work with different versions of StarOffice as well.

Changing page size

When I converted my WordPerfect documents, wpd2sxw failed to save the page size in the newly created OpenOffice.org files. Converted documents all defaulted to Letter, while A4 was used in the original files. This macro sets a document's default page size to A4 (210x297 mm):

Simple as it is, this example demonstrates the extensive use of styles in OpenOffice.org. Here, setting a page's property is accomplished by finding the corresponding style object and modifying that style's properties.

Changing page and paragraph margin size

WordPerfect handles margins in a slightly different way than OpenOffice.org does. In OpenOffice, margin sizes are a page property — paragraph indentation has to be used in order to have paragraphs with different spacing from the left page edge. In contrast, WordPerfect assigns margin sizes to paragraphs — there are no separate entities for page and paragraph margins. Thus, for example, the effect of having a paragraph start 4 centimeters from the left page edge can be represented in exactly one way in WordPerfect (margin = 4 cm for that paragraph), but in many ways in OpenOffice.org (e.g. 2 cm page margin + 2 cm paragraph margin or any other combination which sums up to 4 cm).

wpd2sxw chose to use both margin types for converted documents. Even though all paragraphs had exactly the same left margin sizes (4 cm), in converted documents page margins stayed at the default 2 cm while the other 2 cm were paragraph margins. Of course, I wanted to have the more natural 4 cm page margins and no additional paragraph margins. This macro gets paragraph margin sizes from the first paragraph on the page and makes paragraph margins equal zero while enlarging page margins to compensate for that.

An extra quirk is that wpd2sxw assumed that default page margins in WordPerfect and OpenOffice.org would be the same — which was not quite right. WordPerfect's default happened to be 2.5 cm while OpenOffice's default was 2 cm. I didn't bother to find out how to fix that in the general case; I just added an extra 0.5 cm to the result in order to get the same margin sizes in converted documents as in the original files (but I removed that code from the macro shown here).

Upper and lower margin sizes got lost in the conversion completely, so the best thing I could do was to set them to values based on left and right margin sizes.

Setting page headers and footers

Page headers didn't make it into converted documents, either (current versions of wpd2sxw are capable of converting them correctly, but older versions were not). The macro below adds a header with some text and a few fields (page number and total page count) to the page. Adding footers would be very similar.

This macro also shows how to set basic text attributes such as font name and size or the language for spell-checking and hyphenation.

Setting text alignment

While left, right, and central text alignment were translated correctly, justified paragraphs were converted to left-aligned text. Let's consider an imaginary document template that consists of a centered header and justified text below. Let's suppose the document header contains the company's name and address and its last line is the phone number. The macro should set only the document text's alignment to justified, while leaving the header as is. It should also be able to tell where the header ends even if the text contains an extra space or the phone number has been changed.

One solution is to use a regular expression to find the last line of document header (the one containing the phone number), then loop over all paragraphs below, setting paragraph alignment to justified:

Adding horizontal lines

In order to add a horizontal line below a paragraph of text, one needs to set the paragraph's border style for the bottom border to a solid line. That's exactly what the macro below does to the current paragraph.

Modifying multiple documents

Of course, if we want to use macros for fixing documents broken by converting from another format, we must be able to apply a macro to many documents easily. The macro below reads a list of file names from a text file (which can be generated easily using find) and runs a few macros on each of those files:

Adding macros to OpenOffice menus

Using Tools->Configure... dialog box, you can add items that run macros to OpenOffice.org menus. The name of the menu item added is the name of the subroutine which is executed — which doesn't look very good and can be confusing. Fortunately, you can change menu item names to arbitrary text by editing XML configuration files.

The menu layout for Writer is stored in ~/OpenOffice.orgN.N.N/user/config/soffice.cfg/writermenubar.xml. After creating menu items for your macros using the Configure... dialog, find the corresponding menu items in the abovementioned file and change their menu:label properties to more human-readable strings. All strings must use UTF-8 encoding. Note that this file should not be edited while OpenOffice.org is running — on exiting, OO will overwrite the modified menu layout with its previous version. Also, if you use the OpenOffice Quickstarter, you will need to close it and restart OpenOffice in order for changes in the XML file to take effect.

Conclusion

StarBasic macros saved the day by providing a way of automatically modifying multiple documents and fixing formatting lost during conversion of files from another format. Thanks to this, deficiencies in the converting utility didn't ruin the whole migration plan. If the migration took place today, there would be less need for correcting errors in formatting, thanks to the advancement of wpd2sxw. Of course, OpenOffice.org macros aren't limited to just dealing with text formatting. They are very helpful in customizing the office suite. Unfortunately, sometimes using macros is also necessary for adding functionality many other packages offer built-in (e.g. word-count statistics and binding characters to custom keys). Macros also allow automating often performed actions and are definitely worth your interest if you use OpenOffice.org a lot. Basics of this macro language are quite easy to learn and with all the resources and examples available on the Web, writing your own macros isn't very hard.

History

2004-11-07 — version 1.2; article is double-licensed under the Creative Commons Attribution-ShareAlike License and the GNU Free Documentation License