Programming OpenOffice Calc Macros and Buttons

How to Assign a Macro to a Button in an OpenOffice Calc Spreadsheet

© Mark Alexander Bain

Adding buttons to an OpenOffice Calc spreadsheet will enable users to run macros and will provide them with clean and efficient interface.

An OpenOffice.org Calc spreadsheet can be incredibly useful - improving productivity and reducing both the time and effort needed to carry out convoluted calculations. However, the more complex a spreadsheet becomes the more difficult it is to use. Obviously the person creating the spreadsheet can:

One practical (and neat) solution for the originator of the spreadsheet is to:

Creating Macros for an OpenOffice Calc Spreadsheet

Technically it doesn't actually matter if the designer of the spreadsheet creates all the button for the spreadsheet first and the macros second, or creates the macros first and the buttons first; however, it is always best practice to:

The OpenOffice Basic IDE is, of course, the part of the OpenOffice application used for writing macros (information on use of the IDE can be found in the article "Using Macros to Automate OpenOffice.org Calc"); and this can be used to create the macros that will be used for the buttons; macros like:

Sub date_stamp_click

Dim oCell

oCell = thisComponent.Sheets(1).getCellRangeByName("A1")

oCell.String = Now

End Sub

Sub convert_data_click

Dim oCellIn, oCellOut

oCellIn = thisComponent.Sheets(1).getCellByPosition(0,0)

oCellOut = thisComponent.Sheets(1).getCellByPosition(0,1)

oCellOut.Value = datediff ("s", oCellIn.String, Now)

End Sub

The functionality of these two macros is simple but effective:

There is, however, a subtle difference in the ways in which the two macros select the required cells:

Both methods do exactly the same job, but sometimes it is easier to use the cell coordinates, other times the cell reference.

Adding a Button to the Spreadsheet

Adding a button to a spreadsheet is quite simple:

The result of this part of the process is one or more buttons on the spreadsheet; and the steps needed to make these useful are:

This process needs to repeated for all of the buttons that have been added to the spreadsheet.

Using the Spreadsheet Buttons

At the moment all of the buttons will be useless to a user of the spreadsheet, and that's because they will still be in design mode. To change from design mode to active mode two more steps are required:

With that completed, the OpenOffice Calc user will not have to have an in-depth knowledge of the spreadsheet and it's macros; they will just have clearly visible, and highly useful, buttons to aid them in their daily tasks.


The copyright of the article Programming OpenOffice Calc Macros and Buttons in Office/Business Software is owned by Mark Alexander Bain. Permission to republish Programming OpenOffice Calc Macros and Buttons 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