Programming OpenOffice Calc Custom Dialog Boxes

How to Use Customized Dialogs in OpenOffice.org Calc Macros

© Mark Alexander Bain

This tutorial shows you how to make your OpenOffice Calc users jobs a lot easier by introducing your own custom dialogs and macros.

OpenOffice.org is an invaluable asset on a computer in any home or office; it's Writer component can be used to write any documents (such as a letter to the bank manager, or even an article for Suite101); and the Calc component can be used to produce spreadsheets for everything from daily household cash management to to multi-million dollar business proposals (and hence using Writer to put together a letter to the bank manager). Unfortunately, it doesn't take long for these spreadsheets to become very complicated, often much too complicated for new users; fortunately a spreadsheet designer can make life much easier by introducing custom dialog boxes.

Creating a Custom Dialog Box

There are three stages to creating a custom dialog box:

So, the first job to do is to create the dialog box itself (using the OpenOffice Basic IDE); and to do this click on:

As always in when you're creating a new dialog (or a new Baic module) the IDE will suggest a name (for example Dialog1 or Module1); it's always best to change the name to something more memorable – such as dlgRoomVolume; and once that's done then a new, blank dialog box will have been created. The next step is to:

Once the visual side of the dialog is correct then the functionality needs to be considered.

Writing Code for the Custom Dialog Box

The new dialog box may look professional, but it doesn't actually do anything yet; and so, to make it useful, two things must be done:

At this stage the developer needs to turn to a code window of the IDE - either by clicking on one of the tabs at the bottom of the screen (for an existing macro) or by creating a new macro (as shown in Using Macros to Automate OpenOffice.org Calc).

Obviously, the first task is to write a macro (or macros) that will call the dialog:

Option Explicit

Dim dlgRoomVolume as Object

Sub Main

show_dlgRoomVolume

End Sub

Sub show_dlgRoomVolume

Dim libStandard

Dim libDialog

libStandard = DialogLibraries.getByName("Standard")

libDialog = libStandard.getByName("dlgRoomVolume")

dlgRoomVolume = CreateUnoDialog(libDialog)

dlgRoomVolume.execute

End Sub

This code:

And it's worth noting that it is normally considered bad practice to use global variables whilst programming - in this case it is essential - the dialog box object must be available to all of the macros.

As it stands the user will be able to see the dialog; they'll even be able to type into any text or numeric boxes, but nothing else will happen; therefore, the next job to do is to write the code that will add actions to the dialog:

Sub set_contents

Dim targetCell

targetCell = thisComponent.Sheets(0).getCellRangeByName("B1")

targetCell.Value = dlgRoomVolume.getControl("ipRL").Value

End Sub

The purpose of the macro is to take the contents of numeric boxes in the dialog and to do something with them - in this case to write data to a spreadsheet. However, the dialog still won't 'do' anything - for that to happen the macro needs to be assigned to an action.

Assigning Basic Code to Dialog Events

The custom dialog now consists of two elements:

The macros, therefore, need to be assigned to the actions of the dialog; and this is done by doing the following:

If the macro to show the dialog is now run then the macro will be executed when the button is clicked

Conclusion

This short tutorial has shown how to extend the functionality of OpenOffice by adding custom built dialog boxes. It has shown that the three steps are:

By using this simple technique a developer can quickly build a highly complex, but simple to use, application.


The copyright of the article Programming OpenOffice Calc Custom Dialog Boxes in Office/Business Software is owned by Mark Alexander Bain. Permission to republish Programming OpenOffice Calc Custom Dialog Boxes in print or online must be granted by the author in writing.


A Custom Dialog in the OpenOffice Basic IDE, Mark Alexander Bain
       



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