Operation Manual

end sub
Cursor movements are used for all operations (as opposed to searching). If run on the
DONTKNOW line, the word weight is moved to the front of the line, and the first “The” is changed
to “She”. This is not perfect, but you should not run the macro on the lines that did not have the
proper format. You need to do these manually.
Running a macro quickly
It is tedious to repeatedly run the macro using Tools > Macros > Run Macro on the main menu
bar when the macro can be run from the IDE (Figure 335 on page 337).
1) Go to Tools > Macros > Organize Macros > LibreOffice Basic on the main menu bar to
open the Basic Macro dialog (Figure 333 on page 336).
2) Select your macro and click Edit to open the macro in the IDE.
3) Click the Run BASIC icon on the Macro toolbar, or press the F5 key, to run the macro.
4) Unless you change the first macro, it is the empty macro named Main. Modify Main so that
it reads as shown in Listing 5.
5) Now, you can run CopyNumToCol1 by repeatedly clicking the Run Basic icon in the toolbar
of the IDE. This is very fast and easy, especially for temporary macros that will be used a
few times and then discarded.
Listing 5: Modify Main to call CopyNumToCol1.
Sub Main
CopyNumToCol1
End Sub
Macro recorder failures
Sometimes the macro recorder has a failure and understanding LibreOffice internal workings helps
to understand how and why the macro recorder sometimes fails. The primary offender is related to
the dispatch framework and its relationship to the macro recorder.
Dispatch framework
The purpose of the dispatch framework is to provide uniform access to components (documents)
for commands that usually correspond to menu items. Using File > Save from the main menu bar,
the shortcut keys Ctrl+S, or clicking the Save icon are all of commands that are translated into the
same “dispatch command”.
The dispatch framework can also be used to send “commands” back to the User Interface (UI). 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.
A dispatch command is text, for example .uno:InsertObject or .uno:GoToStartOfLine. The
command is sent to the document frame and this 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 a relatively simple tool to
use 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())
Chapter 13 Getting Started with Macros | 345