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:
| 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:
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.
Note: Comments are owned by the poster. We are not responsible for their content.
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?
Which version of OOo
Posted by: Iain Roberts on February 24, 2005 08:08 PM#