Linux.com

Feature

Basic button-pushing with OpenOffice.org macros

By Rob Reilly on February 24, 2005 (8:00:00 AM)

Share    Print    Comments   

OpenOffice.org is gaining popularity in the corporate mainstream, yet one of its most powerful features, macros, can be pretty intimidating to new users. Let's see how easy it is to create an OpenOffice.org macro and connect it to a simple pushbutton.

There are two ways to create a macro in OOo. One is to use OpenOffice.org Basic to write the macro. The other is to use the macro recorder. That will be the approach we focus on.

The macro recorder is great, because it lets you create a macro without any programming, and when you're done you can look at the code it built and add your own enhancements.

We'll sort a grocery list to illustrate how to build macros. I update my OpenOffice.org Calc-created grocery list spreadsheet weekly before trudging off to the store. I don't know how some of you shoppers do it with your handwritten random lists.

Before I run my macro, I delete the quantity of each item from the previous week. I sort the list alphabetically by grocery item (column A), then enter the desired number of each grocery item (column B). Once I've done that data entry, I want to sort the list from lowest to highest according to aisle (column C), filter the list so only non-zero-quantity items show up, then print the filtered list.

I created a macro to sort by item name using the macro recorder:

  • Select the Tools menu item, then Macros.
  • Click Record Macro to begin to record your keystrokes.
  • Left-click on the first item in column A.
  • Drag the mouse down to the bottom of the list, then across to include columns B and C.
  • Click the Data menu item, then Sort.
  • Select Column A and Ascending.
  • Click OK to do the sort.
  • Click the Stop Recording button that popped up when you clicked Record Macro. The recording box will close and open a menu for specifying the macro name. Click My Macros, then Standard, and finally Modules1. Move the cursor up to the upper left input box and give the new macro a reasonable name. Since I was sorting on the A column, I called the macro "sorta."
  • Finish up by clicking OK.
Why macros?

Why would you want to use macros? If you do repetitive jobs, like moving data around in a spreadsheet or regularly deleting old data from a column, some simple macros can save you lots of time and reduce your error rate. Automating tasks in OpenOffice.org might just turn you into the departmental macro guru, and managers and business owners like people who can make using spreadsheets faster and easier.

Running the macro is even easier than creating it. Step through the Tools menu, Macro, and Run Macro. Pick the macro out of the list and push the Run button at top right. In my case it was My Macros, Standard, Module1, and "sorta." The spreadsheet flashed briefly and then it was sorted alphabetically by column A.

Creating a macro to sort by aisle was the same process, except I sorted on Column C instead of Column A and named it "sortc."

I also created a "finddeli" macro that looks for all instances of the word "deli" in my list. You can record just about any sequence of actions or key clicks and turn them into a macro.

Attaching macros to buttons

Clicking through the Tools, Macro, Run Macro sequence is almost as much effort as just sorting manually. A worthwhile upgrade I made was to attach the sorta macro to a button that could be placed right on the spreadsheet:

  • Turn the control toolbar on with View, Toolbars, and Controls. The floating toolbar will appear.
  • Click the Design Mode On/Off button (the ruler with the little draftsman's triangle) on the Controls toolbar to light up the various controls. Click the pushbutton and then move down to the spreadsheet and use the mouse to drag out a rectangle.
  • Right-click on the new button, then select the Controls menu item to bring up the button properties menu.
  • On the General tab fill in an appropriate Label for the button. In my case it was "sort a."
  • On the Events tab move down to the Mouse Pressed item and click the triple dot button on the right.
  • On the Assign Menu, click the Assign button to bring up the Macro Selector menu, where you can choose the macro to be actuated by the button. In my case I chose My Macros, Standard, Module1, and the sorta macro.
  • Click OK to complete the assignment.
  • Again click the Design Mode On/Off button to allow the button to be pushed in the spreadsheet.

You can now run the sorting macro by clicking on the button.

Creating buttons and macros for simple repetitive jobs like this can save you loads of time. You might look at your spreadsheets and make a list of the tasks that you do over and over, then record a macro and run it to see if it saves you some time. Any situation where you flip back and forth between some spreadsheet state is a candidate for some pushbutton automation.

If you want to get more sophisticated with your spreadsheets, you can also use text boxes, radio buttons, and list boxes. Controls like buttons and list boxes on forms are another way to interface with macros.

For a thorough education on OpenOffice.org macros be sure to get "OpenOffice.org Macros Explained" by Andrew Pitonyak. Don't let the book's massive 476 pages intimidate you. It has vast sections of basic programming practice that explain things in minute detail. It could be a knowledgeable silent companion for anybody who wants to be a departmental OpenOffice.org macro guru.

Rob Reilly is a consultant, writer, and commentator who advises clients on business and technology projects. His Linux, portable computing, and public speaking skills-related articles regularly appear in various high-end Linux and business media outlets.

Share    Print    Comments   

Comments

on Basic button-pushing with OpenOffice.org macros

Note: Comments are owned by the poster. We are not responsible for their content.

Which version of OOo

Posted by: Iain Roberts on February 24, 2005 08:08 PM
The instructions in the article worked fine for me - but only in OOo v2 (well, 1.9.79). In OOo 1.1.2 the details were a bit different (and the macro failed with an error about no dispatcher available, but that's probably just me).

#

Works with 1.1.4

Posted by: Anonymous Coward on February 27, 2005 07:03 AM
Problems make sure you are using current there are a lot of fixs even in a minor number change.

#

What proactive action is taken against viruses?

Posted by: Anonymous Coward on February 24, 2005 09:39 PM
Macros can be very useful, but what proactive action is taken to make viruses impossible?


I mean we have a chance to learn from the catastrophic failures of MS in the area of macro virus. Most people have turned off macros in MS-Office since way back. What's to prevent the same from happening in OOo?

#

Re:What proactive action is taken against viruses?

Posted by: Anonymous Coward on February 25, 2005 03:06 AM
You can disable macros in OpenOffice.org. When you open a document containing macros it will ask you if you want to enable or disable before you open it.

#

Re:What proactive action is taken against viruses?

Posted by: Anonymous Coward on February 25, 2005 05:41 AM
> it will ask you if you want to
> enable or disable before you open it.

This is not a solution. On what shall I base my choice? It's Russian roulette: either I say 'yes' and take a blind chance that the macros are harmless, or say 'no' and expect that some feature of the document will be missing.

#

Depends how openoffice is setup.

Posted by: Anonymous Coward on February 25, 2005 07:11 AM
Directorys can be defined as the only place files contatian macros can work from.

Final is a simple anti-virus/anti-macro. Since all script files must be registed in one directory of a Openoffice file that is a zip file just have a script delete the directory Basic from every OpenOffice file and you have clean OpenOffice files. Yes you lose the macros but not the data. Note even renaming the directory inside the Zip will stop the macros.

Far simpler virus responce. Word being verry closed about there internal formats makes the problem hard to deal with.(Yes I do have a openoffice macro remove or disable system)

It does not take long using standard tools to create the script that either delete or renames the macro directory.

Could anyone tell me why you would need to get a Macro from a untrusted source(Email).

Microsoft Office offers the same option as OpenOffice expect Microsoft Office files are harder to clean.

Restore from backup hmm do we have infected files. With OpenOffice I can be sure that we don't have any infect OpenOffice files after replacing OpenOffice and striping the macros.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya