129. Create and Use a Macro
BEFORE YOU BEGIN
128 About OpenOffice.org Macros
133 Associate OpenOffice.org and Microsoft Office Files
The process of recording a macro (a recorded sequence of commands) is mainly comprised of performing the commands as you normally would, only with the recorder turned on. The trick to making a proper recording, however, is setting up your document beforehand so that the commands you're recording can apply to any document you may be editing in the future, not just the one you're looking at now.
After recording is completed, it's actually a bit trickier to assign that macro to a shortcut key . When that's finally done, you can type a single keystroke to have your OpenOffice.org application replay that macro very, very quickly.
Shortcut key A keystroke, such as Ctrl+Shift+B , that represents a command otherwise located within an onscreen menu or toolbar.
Prepare Your Document
In either Writer of Calc, set up your document or spreadsheet to appear exactly as it should before you would normally execute the first action you intend to record as a macro. For example, if your macro is to change the formatting of a selected paragraph in Writer, select a paragraph now, and let that be the "guinea pig" selection for your macro recording.
To begin recording, choose Tools, Macros, Record Macro . The Record Macro toolbar appears, containing a single button, Stop Recording .
| | Perform Actions to Be Recorded
Carefully reproduce all the actions you want the macro to repeat for you. The recorder takes note of every command you execute through the menu bar, a toolbar, or a keystroke shortcut. It will not
record actions made with the mouse directly to an open document, such as selecting a graphic on a page, clicking a different cell , or selecting a passage of text, so whenever possible, use the keyboard to perform such actions (for more, see 128 About OpenOffice.org Macros
129. Create and Use a Macro
When you're satisfied that your sequence of commands has been completely demonstrated for the recorder, click Stop Recording . If the recorder has taken note of any commands that can be saved to a macro, the OpenOffice.org Basic Macros dialog box appears; otherwise, recording stops with no response from the application.
| | Name and Save the Macro
In the Macro name
text box, enter a name for your macro. In place of spaces, use an underscore , such as Format_Heading
. It is perfectly safe to leave recorded macros in the Module1
module of the Standard
library in the My Macros
volume. If you'd rather store the macro along with an open documentthus ensuring that the macro is used only with that documentchoose that document from the Save macro in
list. To store the macro, click Save
When the OpenOffice.org Basic Macros dialog box first appears, the default choice in the Save macro in list is the name of the module from which you last executed a macroeven if you cannot or should not store your newly recorded macro in that location. Make sure that you choose a safe location, such as Module1 of the Standard library in My Macros . For more on macro locations, see 128 About OpenOffice.org Macros .
Test the Macro
This is a good time to make certain that the macro works the way you intend. Prepare a documenteither the open one or, in the case of macros saved to the Standard library, a new documentwhose conditions approximate those from which you'll normally use your macro. Select Tools, Macros, Run Macro . The Macro Selector dialog box appears. From the Library list, choose the location where your macro was stored. Unfortunately, this choice is not made automatically, so you must recall the location manually (by default, My Macros, Standard, Module1 ). In the Macro name list, choose the name of your recorded macro, and then click Run .
The commands recorded in the macro are executed. You will not notice each step of the execution; you will only see the results of the macro, instantaneously. This is because OpenOffice.org stops updating its own windows when executing a macro, which speeds things up tremendously.
Examine the results closely. If something appears amiss, repeat the test. If you don't get the results you intend after a second test, you can record the macro over again. Repeat this procedure from step 1, but in step 5, instead of entering a new name, choose the bad macro's name from the Existing macros in list, and click Save . A dialog box asks you to confirm overwriting the existing macro; click Yes .
Assign a Shortcut Key to the Macro
When the recorded macro works perfectly in simulated work conditions, you can assign it to a command or shortcut key so that you don't have to plow through libraries to get to it. Select Tools, Macros, Organize Macros, OpenOffice.org Basic . The OpenOffice.org Basic Macros dialog box appears. You do not have to choose your macro's name at this point; instead, just click Assign . The Customize dialog box appears. Here, you can determine how the macro is to be triggered in the future.
For example, to assign the macro to a shortcut key, click the Keyboard tab. To make this key execute the macro in all OpenOffice.org applications, click OpenOffice.org in the upper-right corner; otherwise, click the name of the application from which you recorded the macro.
The Shortcut keys list shows all possible keystrokes; those to which commands are presently assigned have the associated commands listed in the rightmost column. Unavailable keystrokes are shown in gray. Preferably, you should choose an unassigned keystroke from this list.
In the Category list, choose OpenOffice.org Macros, user , followed by the location of your recorded macro (for instance, Standard, Module1 ). In the Function list, choose the macro to which you want to assign the keystroke. Then click Modify . The name of your macro should appear in the right column of the Shortcut keys list, and the chosen keystroke should appear in the Keys list. Click OK to finalize this assignment, and then click Close .
Elsewhere in the Customize dialog box, the Menus tab enables you to assign macros to menu commands. The Toolbars tab lets you create new perennially visible buttons for your macro. Finally, the Events tab is where you can assign a macro to be triggered by a specific event, such as every time a document is saved. This way, for instance, you can create a "diagnostic" macro that makes certain your document contains a feature you might specifically require before the document is saved to disk.
If you need to make certain that nothing is selected before the main body of your macro proceedsfor instance, so that anything your macro might add to a document doesn't overwrite existing textfor the first keystroke in your recording, press the Esc key.