Using Macros to Automate OpenOffice.org Calc

How to Create an OpenOffice Macro

© Mark Alexander Bain

OpenOffice.org Calc can be automated very easily by introducing macros - simple scripts that will do all of those laborious tasks

OpenOffice.org is fast becoming, if not a replacement then, at least an alternative to Microsoft Office. Like Microsoft Office OpenOffice.org has:

With OpenOffice.org Basic it is possible for anyone to extend the default functionality and automate any essential or repetitive tasks.

OpenOffice.org Basic Macro Organization

Before creating an OpenOffice.org basic macro the new user needs to understand a little about how macros are organized:

Bearing all of this in mind the new user is ready to create their first macro.

Creating a New OpenOffice.org Basic Macro

OpenOffice.org Basic has its own built in IDE (Integrated Development Environment). So if Calc, for example, is open then the first steps to creating a new macro are:

OpenOffice.org will now display the 'OpenOffice.org Basic Macros' dialog box and the new user must decide which area to use for the macro (i.e. 'My Macros' or the document itself) and then pressing the 'New' button. At this point a new macro will be created and the IDE will open up in edit mode.

It's worth noting that OpenOffice.org always creates a subroutine called 'Main', but this has nothing to do with the 'Main' subroutine seen in programming languages such as C (where 'Main' is the subroutine that is run by default); OpenOffice.org always runs the first macro that it comes to, regardless of its name.

The real strength of the OpenOffice.org Basic macro is when it comes to automating tasks that would otherwise take up valuable time and effort (although it's always worth considering whether or not the time spent coding outweighs the time that it's meant to save); and the required automation can be achieved with relatively few, simple concepts.

Writing OpenOffice.org Basic Macro Code

Any OpenOffice.org application consists of a number of objects and functions:

The end result is a working macro:

Sub Main

Dim oSheet

oSheet = thisComponent.Sheets(0)

oSheet.getCellRangeByName("A1").String = "Input Data"

oSheet.getCellRangeByName("A2").Value = 4

oSheet.getCellRangeByName("A3").Value = 5

oSheet.getCellRangeByName("A4").Formula = "=A2*A3"

End Sub

Running an OpenOffice.org Basic Macro

The macro is run by pressing the 'Run Macro' button (the one with the green triangle) and (assuming all the lines have been entered correctly) then the spreadsheet will have:

This is, of course, a very simple example, but does show how easily tasks can be automated within OpenOffice.org.


The copyright of the article Using Macros to Automate OpenOffice.org Calc in Office/Business Software is owned by Mark Alexander Bain. Permission to republish Using Macros to Automate OpenOffice.org Calc in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo