12 April 2014: The OpenOffice Wiki is not, and never was, affected by the heartbleed bug. Users' passwords are safe and wiki users do not need take any actions.

How to run a macro

From Apache OpenOffice Wiki
Jump to: navigation, search


A typical method to run a macro is as follows:

  1. Use Tools > Macros > Run Macro to open the Macro Selector dialog).
  2. Select the library and module in the Library list (left hand side).
  3. Select the macro in the Macro name list (right hand side).
  4. Click Run to run the macro.
Use the Macro Selector dialog to run macros.

Although you can use Tools > Macros > Run Macro to run all macros, this is not efficient for frequently run macros. A more common technique is to assign a macro to a toolbar button, menu item, keyboard shortcut, or a button embedded in a document. While choosing a method, it is also good to ask questions such as:

  • Should the macro be available for only one document, or globally for all documents?
  • Does the macro pertain to a specific document type, such as a Calc document?
  • How frequently will the macro be used?

The answers will determine where to store the macro and how to make it available. For example, you will probably not add a rarely used macro to a toolbar. To help determine your choices, see Table 2.

Table 2. Methods for starting a macro.

Type OpenOffice.org Document Type Document
Toolbar No Yes Yes
Menu No Yes Yes
Shortcut Yes Yes No
Event Yes No Yes

To add a menu item, keyboard shortcut, or toolbar icon that calls a macro, use the Customize dialog. Open this dialog in either of these ways:

  • Choose Tools > Customize from the main menu bar.
  • Each toolbar has an icon Graphics11.png that opens a menu; choose the Customize Toolbar option.
OpenOffice.org Customize dialog.
Tip.png Complete coverage of the Customize dialog is beyond the scope of this document. Click the Help button to access the help pages included with OpenOffice.org.


The Customize dialog contains tabs to configure menus, keyboard bindings, toolbars, and events.

Toolbar

Macros can be added to toolbars. To see more about modifying toolbars, see Chapter 4 (Menus and Toolbars).

Menu item

Use Tools > Customize to open the Customize dialog, and select the Menus tab. You can modify an existing menu, or create new menus that call macros. To see more about modifying menus, see Chapter 4 (Menus and Toolbars).

Keyboard shortcuts

Use Tools > Customize to open the Customize dialog, and select the Keyboard tab. Assigning keyboard shortcuts is discussed in Appendix A (Keyboard Shortcuts).

Event

In OpenOffice.org, when something happens, we say that an event occurred. For example, a document was opened, a key was pressed, or the mouse moved. OpenOffice.org allows events to cause a macro to be called; the macro is then called an event handler. Full coverage of event handlers is well beyond the scope of this document, but a little knowledge can accomplish much.

Documentation caution.png Be careful when you configure an event handler. For example, assume that you write an event handler that is called every time that a key is pressed, but you make a mistake so the event is not properly handled. One possible result is that your event handler will consume all key presses, forcing you to forcibly terminate OpenOffice.org.


Use Tools > Customize to open the Customize dialog, and select the Events tab. The events in the Customize dialog are related to the entire application and specific documents. Use the Save In box to choose OpenOffice.org, or a specific document.

Assign macro to an application level event.

A common use is to assign the Open Document event to call a specific macro. The macro then performs certain setup tasks for the document. Select the desired event and click the Macro button to open the Macro Selector dialog.

Assign macro to the document open event.

Select the desired macro and click OK to assign the macro to the event. The Events tab shows that the event has been assigned to a macro. When the document opens, the PrintHello macro is run.

Print Hello is assigned to the Open Document event.

Many objects in a document can be set to call macros when events occur. The most common usage is to add a control, such as a button, into a document. Even double-clicking on a graphic opens a dialog with a Macros tab that allows you to assign a macro to an event.


Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools