Sometimes the macro recorder fails

From Apache OpenOffice Wiki
Jump to: navigation, search

Understanding the internals helps to understand how and why the macro recorder frequently fails. The primary offender is related to the dispatch framework and its relationship to the macro recorder.

The dispatch framework

The purpose of the dispatch framework is to provide a uniform access to components (documents) for commands that usually correspond to menu items. I can use File > Save from the menu, the shortcut keys Ctrl+S, or click on the Save toolbar icon. All of these commands are translated into the same “dispatch command", which is sent to the current document.

The dispatch framework can also be used to send “commands" back to the UI (User Interface). For example, after saving the document, the File Save command is disabled. As soon as the document has been changed, the File Save command is enabled.

If we see a dispatch command, it is text such as .uno:InsertObject or .uno:GoToStartOfLine. The command is sent to the document's frame, and the frame passes on the command until an object is found that can handle the command.

How the macro recorder uses the dispatch framework

The macro recorder records the generated dispatches. The recorder is relatively simple to implement and the same commands that are issued are recorded for later use. The problem is that not all dispatched commands are complete. For example, inserting an object generates the following code:

dispatcher.executeDispatch(document, ".uno:InsertObject", "", 0, Array())

It is not possible to specify what kind of object to create or insert. If an object is inserted from a file, you cannot specify which file to insert.

I recorded a macro and used Tools > Options to open and modify configuration items. The generated macro does not record any configuration changes; in fact, the generated code is commented so it will not even be run.

rem dispatcher.executeDispatch(document, ".uno:OptionsTreeDialog", "", 0, Array())

If a dialog is opened, the command to open the dialog is likely to be generated. Any work done inside the dialog is not likely to be recorded. Examples include macro organization dialogs, inserting special characters, and similar types of dialogs. Other possible problems using the macro recorder include things such as inserting a formula, setting user data, setting filters in Calc, actions in database forms, and exporting a document to an encrypted PDF file. You never know for certain what will work unless you try it, however. The actions from the search dialog are properly captured, for example.

Other options

When the macro recorder is not able to solve a specific problem, the usual solution is to write code using the objects. Unfortunately, there is a steep learning curve for the OOo objects. It is usually best to start with simple examples and then branch out slowly as you learn more. Learning to read generated macros is a good place to start.

If you record Calc macros, and the recorder can correctly generate a macro, there is an add-in created by Paolo Mantovani, which converts Calc macros when they are recorded. The final code manipulates objects rather than generating dispatches. This can be very useful for learning the object model.

You can download the macro recorder from Paolo's web site directly or from the OOo Macros web site. You should check both places to see which contains the latest version.

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