Difference between revisions of "IT/Documentation/OOo3 User Guides/Getting Started/Creating a macro"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Creazione di una macro)
 
Line 1: Line 1:
 +
{{DISPLAYTITLE:Creazione di una macro}}
 +
{{Documentation/GS3MacroITTOC
 +
|ShowPrevNext=block
 +
|PrevPage=IT/Documentation/OOo3 User Guides/Getting Started/Creating a simple macro
 +
|NextPage=IT/Documentation/OOo3 User Guides/Getting Started/Sometimes the macro recorder fails
 +
}}__NOTOC__
 +
 +
 +
 +
 +
 +
 +
 +
 +
Traduzione in corso - Translation in progress
 +
 +
 +
 +
 +
 +
 +
 +
 
Creazione di una macro
 
Creazione di una macro
  
Line 160: Line 183:
  
 
Adesso, potete eseguire CopiaNumAllaCol1 facendo clic ripetutamente sull'icona '''Esegui Programma Basic''' nella barra degli strumenti dell'IDE. Questo metodo è semplice e rapido, specialmente per macro temporanee che vengono utilizzate solo poche volte e poi cancellate.
 
Adesso, potete eseguire CopiaNumAllaCol1 facendo clic ripetutamente sull'icona '''Esegui Programma Basic''' nella barra degli strumenti dell'IDE. Questo metodo è semplice e rapido, specialmente per macro temporanee che vengono utilizzate solo poche volte e poi cancellate.
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
A recorded macro repeats the same task over and over again. Before creating a recorded macro, I usually ask two questions:
 +
 +
# Can the task be summarized as a simple set of commands that do not change?
 +
# Can the steps be arranged such that the last command leaves the cursor ready for the next command?
 +
 +
==A complicated example==
 +
 +
I frequently copy rows and columns of data from a web site and format them as a table in a text document. First, I copy the table from the web site to the clipboard. To avoid strange formatting and fonts, I paste the text into a Writer document as unformatted text. I reformat the text with tabs between columns so that I can use '''Table > Convert > Text to Table''' to convert to a table.
 +
 +
I inspect the text to see if I can record a macro to format the text (remember the two questions that I ask). As an example, I copied the FontWeight constants group from the OpenOffice.org web site. The first column indicates the constant name. Each name is followed by a space and a tab.
 +
 +
{| border="0" cellpadding="0"
 +
|-
 +
| DONTKNOW||The font weight is not specified/known.
 +
|-
 +
| THIN||specifies a 50% font weight.
 +
|-
 +
| ULTRALIGHT   ||specifies a 60% font weight.
 +
|-
 +
| LIGHT||specifies a 75% font weight.
 +
|-
 +
| SEMILIGHT||specifies a 90% font weight.
 +
|-
 +
| NORMAL||specifies a normal font weight.
 +
|-
 +
| SEMIBOLD||specifies a 110% font weight.
 +
|-
 +
| BOLD||specifies a 150% font weight.
 +
|-
 +
| ULTRABOLD||specifies a 175% font weight.
 +
|-
 +
| BLACK||specifies a 200% font weight.
 +
|-
 +
|}
 +
 +
I want the first column to contain the numeric value, the second column the name, and the third column the description. The desired work is easily accomplished for every row except for DONTKNOW and NORMAL, which do not contain a numeric value—but I know that the values are 0 and 100, so I will enter those manually.
 +
 +
The data can be cleaned in multiple ways—all of them easy. The first example uses keystrokes that assume the cursor is at the start of the line with the text THIN.
 +
 +
# Use '''Tools > Macros > Record Macro''' to start recording.
 +
# Press ''Ctrl+Right Arrow'' to move the cursor to the start of “specifies".
 +
# Press ''Backspace'' twice to remove the tab and the space.
 +
# Press ''Tab'' to add the tab without the space after the constant name.
 +
# Press ''Delete'' to delete the lower case s and then press ''S'' to add an upper case S.
 +
# Press ''Ctrl+Right Arrow'' twice to move the cursor to the start of the number.
 +
# Press ''Ctrl+Shift+Right Arrow'' to select and move the cursor before the % sign.
 +
# Press ''Ctrl+C'' to copy the selected text to the clipboard.
 +
# Press ''End'' to move the cursor to the end of the line.
 +
# Press ''Backspace'' twice to remove the two trailing spaces.
 +
# Press ''Home'' to move the cursor to the start of the line.
 +
# Press ''Ctrl+V'' to paste the selected number to the start of the line.
 +
# Pasting the value also pasted an extra space, so press ''Backspace'' to remove the extra space.
 +
# Press ''Tab'' to insert a tab between the number and the name.
 +
# Press ''Home'' to move to the start of the line.
 +
# Press ''down arrow'' to move to the next line.
 +
# Stop recording the macro and save the macro.
 +
 +
It takes much longer to read and write the steps than to record the macro. Work slowly and think about the steps as you do them. With practice this becomes second nature.
 +
 +
The generated macro has been modified to contain the step number in the comments to match the code to the step above.
 +
 +
''Listing 2: Copy the numeric value to the start of the column.''
 +
 +
<code>
 +
  sub CopyNumToCol1
 +
  rem ----------------------------------------------------------------------
 +
  rem define variables
 +
  dim document  as object
 +
  dim dispatcher as object
 +
  rem ----------------------------------------------------------------------
 +
  rem get access to the document
 +
  document  = ThisComponent.CurrentController.Frame
 +
  dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 +
 
 +
  rem (2) Press Ctrl+Right Arrow to move the cursor to the start of "specifies".
 +
  dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
 +
 
 +
  rem (3) Press Backspace twice to remove the tab and the space.
 +
  dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 +
 
 +
  rem ----------------------------------------------------------------------
 +
  dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 +
 
 +
  rem (4) Press Tab to add the tab without the space after the constant name.
 +
  dim args4(0) as new com.sun.star.beans.PropertyValue
 +
  args4(0).Name = "Text"
 +
  args4(0).Value = CHR$(9)
 +
 
 +
  dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args4())
 +
 
 +
  rem (5) Press Delete to delete the lower case s ....
 +
  dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())
 +
 
 +
  rem (5) ... and then press S to add an upper case S.
 +
  dim args6(0) as new com.sun.star.beans.PropertyValue
 +
  args6(0).Name = "Text"
 +
  args6(0).Value = "S"
 +
 
 +
  dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args6())
 +
 
 +
  rem (6) Press Ctrl+Right Arrow twice to move the cursor to the number.
 +
  dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
 +
 
 +
  rem ----------------------------------------------------------------------
 +
  dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
 +
 
 +
  rem (7) Press Ctrl+Shift+Right Arrow to select the number.
 +
  dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())
 +
 
 +
  rem (8) Press Ctrl+C to copy the selected text to the clipboard.
 +
  dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
 +
 
 +
  rem (9) Press End to move the cursor to the end of the line.
 +
  dispatcher.executeDispatch(document, ".uno:GoToEndOfLine", "", 0, Array())
 +
 
 +
  rem (10) Press Backspace twice to remove the two trailing spaces.
 +
  dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 +
 
 +
  rem ----------------------------------------------------------------------
 +
  dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 +
 
 +
  rem (11) Press Home to move the cursor to the start of the line.
 +
  dispatcher.executeDispatch(document, ".uno:GoToStartOfLine", "", 0, Array())
 +
 
 +
  rem (12) Press Ctrl+V to paste the selected number to the start of the line.
 +
  dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
 +
 
 +
  rem (13) Press Backspace to remove the extra space.
 +
  dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 +
 
 +
  rem (14) Press Tab to insert a tab between the number and the name.
 +
  dim args17(0) as new com.sun.star.beans.PropertyValue
 +
  args17(0).Name = "Text"
 +
  args17(0).Value = CHR$(9)
 +
 
 +
  dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args17())
 +
 
 +
  rem (15) Press Home to move to the start of the line.
 +
  dispatcher.executeDispatch(document, ".uno:GoToStartOfLine", "", 0, Array())
 +
 
 +
  rem (16) Press down arrow to move to the next line.
 +
  dim args19(1) as new com.sun.star.beans.PropertyValue
 +
  args19(0).Name = "Count"
 +
  args19(0).Value = 1
 +
  args19(1).Name = "Select"
 +
  args19(1).Value = false
 +
 
 +
  dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args19())
 +
  end sub
 +
</code>
 +
 +
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 I should not have run the macro on the lines that did not have the proper format; I need to do these manually.
 +
 +
==Running the macro quickly==
 +
 +
It is tedious to repeatedly run the macro using '''Tools > Macros > Run Macro''' (see Figure 3). The macro can be run from the IDE. Use '''Tools > Macros > Organize Macros > OpenOffice.org Basic''' to open the Basic Macro dialog. Select your macro and click '''Edit''' to open the macro in the IDE.
 +
 +
The IDE has a '''Run Basic''' icon in the toolbar that runs the first macro in the IDE. Unless you change the first macro, it is the empty macro named Main. Modify Main so that it reads as shown in Listing 3.
 +
 +
''Listing 3: Modify Main to call CopyNumToCol1.''
 +
 +
<code>
 +
  Sub Main
 +
    CopyNumToCol1
 +
  End Sub
 +
</code>
 +
 +
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.
 +
 +
{{Manual}}
 +
[[Category:Getting Started (Documentation)]]

Revision as of 15:00, 13 July 2010







Traduzione in corso - Translation in progress





Creazione di una macro

Di solito mi faccio due domande, prima di registrare una macro:

  1. Il compito può essere eseguito come un semplice insieme di comandi?
  2. Posso organizzare i passaggi in maniera tale che l'ultimo comando posizioni il cursore in modo da accettare direttamente il prossimo comando?

Un esempio complesso

Di solito copio righe e colonne di dati da un sito web e li formatto in una tabella, in un documento di testo. Prima di tutto, copio la tabella dal sito web negli appunti. Per evitare di copiare anche la formattazione e il tipo di carattere, incollo il testo in un documento Writer come testo non formattato. Riformatto il testo con l'utilizzo delle tabulazioni, per utilizzare Tabella > Converti > Testo in tabella per convertirlo in una tabella.

Analizzo il testo, per vedere se posso registrare una macro che formatti il testo (ricordando le due domande che mi sono posto). Come esempio, ho copiato il gruppo di costanti FontWeight dal sito web OpenOffice.org. La prima colonna indica il nome delle costanti. Ogni nome è seguito da uno spazio e una tabulazione.

<center>DONTKNOW Il peso della costante non viene specificato.
THIN Identifica una dimensione del font del 50% .
ULTRALIGHT Identifica una dimensione del carattere del 60% .
LIGHT Indica una dimensione del carattere del 75%.
SEMILIGHT Identifica una dimensione del carattere del 90% .
NORMAL Identifica una dimensione standard del carattere.
SEMIBOLD Identifica una dimensione del carattere del 110% .
BOLD Identifica una dimensione del carattere del 150% .
ULTRABOLD Identifica una dimensione del carattere del 175% .
BLACK Identifica una dimensione del carattere del 200% .
</center>

Voglio che la prima colonna contenga il valore numerico, la seconda colonna il nome, e la terza la descrizione. Il lavoro è facilmente eseguibile per ogni riga, eccetto per DONTKNOW e NORMAL, che non contengono un valore numerico, ma so che i valori sono rispettivamente 0 e 100, quindi posso inserirli manualmente.

I dati possono essere ripuliti in molti modi—tutti semplici. Il primo esempio utilizza la pressione di opportuni tasti presumendo che il cursore si trovi all'inizio della riga con il testo THIN.

  1. Fate clic su Strumenti > Macro > Registra Macro per cominciare la registrazione di una macro.
  2. Premete Ctrl+Freccia destra per muovere il cursore all'inizio delle specifiche.
  3. Premete Backspace due volte per rimuovere lo spazio e la tabulazione.
  4. Premete Tab per aggiungere una tabulazione senza lo spazio dopo il nome della costante.
  5. Premete Canc per cancellare la s minuscola, e dopo premete S per aggiungere una S maiuscola.
  6. Premete Ctrl+Freccia destra due volte per muovere il cursore all'inizio del numero.
  7. Premete Ctrl+Maiusc+Freccia destra per selezionare e muovere il cursore prima del simbolo %.
  8. Premete Ctrl+C per copiare il testo selezionato negli appunti.
  9. Premete Fine per muovere il cursore alla fine della riga.
  10. Premete Backspace due volte per rimuovere gli spazi rimanenti.
  11. Premete Home Per muovere il cursore all'inizio della riga.
  12. Premete Ctrl+V per incollare il numero selezionato all'inizio della riga.
  13. Quando incollate il valore, verrà incollato anche uno spazio extra, quindi premete Backspace per rimuovere lo spazio che avanza.
  14. Premete Tab per inserire una tabulazione fra il numero e il nome.
  15. Premete Home per muovervi all'inizio della riga.
  16. Premete Freccia giù per muovere il cursore alla riga successiva.
  17. Fermate la registrazione e salvate la macro.

Ci vuole molto più tempo per leggere i passi da eseguire che per eseguire la macro. Lavorate con calma e pensate ai passi e a come li fareste voi. Con la pratica verrà tutto più naturale.

La macro generata è stata modificata per contenere il numero del passo nei commenti, per vedere la corrispondenza con le azioni descritte sopra.

Listato 1: Copia del valore numerico all'inizio della colonna.


sub CopyNumToCol1
rem -------------------------------------------------------------
rem definizione variabili
dim document   as object
dim dispatcher as object
rem -------------------------------------------------------------
rem accesso al documento
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem (2) Premete Ctrl+Freccia destra per muovere il cursore all'inizio di “specifies”.
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
rem (3) Premete Backspace due volte per rimuovere lo spazio e la tabulazione.
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
rem -------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
rem (4) Premete Tab per aggiungere una tabulazione senza lo spazio dopo il nome della costante.
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "Text"
args4(0).Value = CHR$(9)
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args4())
rem (5) Premete Canc per cancellare la s minuscola....
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())
rem (5) ... e dopo premete S per aggiungere una S maiuscola.
dim args6(0) as new com.sun.star.beans.PropertyValue
args6(0).Name = "Text"
args6(0).Value = "S"
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args6())
rem (6) Premete Ctrl+Freccia destra due volte per muovere il cursore all'inizio del numero.
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
rem -------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
rem (7) Premete Ctrl+Shift+Freccia destra per selezionare il numero.
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())
rem (8) Premete Ctrl+C per copiare il testo selezionato negli appunti.
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
rem (9) Premete Fine per muovere il cursore alla fine della riga.
dispatcher.executeDispatch(document, ".uno:GoToEndOfLine", "", 0, Array())
rem (10) Premete Backspace due volte per rimuovere gli spazi rimanenti.
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
rem -------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
rem (11) Premete Home per muovere il cursore all'inizio della riga.
dispatcher.executeDispatch(document, ".uno:GoToStartOfLine", "", 0, Array())
rem (12) Premete Ctrl+V per incollare il numero selezionato all'inizio della riga.
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
rem (13) Premete Backspace per rimuovere lo spazio rimasto.
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
rem (14) Premete Tab per inserire una tabulazione fra il numero ed il nome.
dim args17(0) as new com.sun.star.beans.PropertyValue
args17(0).Name = "Text"
args17(0).Value = CHR$(9)
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args17())
rem (15) Premete Home per andare all'inizio della riga.
dispatcher.executeDispatch(document, ".uno:GoToStartOfLine", "", 0, Array())
rem (16) Premete Freccia giù per muovere il cursore alla riga successiva.
dim args19(1) as new com.sun.star.beans.PropertyValue
args19(0).Name = "Count"
args19(0).Value = 1
args19(1).Name = "Select"
args19(1).Value = false
dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args19())
end sub

I movimenti del cursore vengono utilizzati per tutte le operazioni (al contrario di quanto accade nella ricerca). Se eseguite la macro nella riga contenente DONTKNOW, la parola weight viene spostata davanti alla riga e il primo “The” viene cambiato in “She”. In questo caso la macro non funziona bene, ma non dovrei eseguire la macro nelle righe che non hanno il formato appropriato: bisogna elaborarle manualmente.

Esecuzione rapida di una macro

È tedioso eseguire ripetutamente una macro facendo sempre clic su Strumenti > Macro > Esegui Macro (vedere Errore: sorgente del riferimento non trovata). Potete eseguire la macro dall'interno dell'IDE. Fate clic su Strumenti > Macro > Organizza Macro > OpenOffice.org Basic per aprire la finestra di dialogo Macro OpenOffice.org Basic. Selezionate la vostra macro e fate clic su Modifica per aprire la macro nell'IDE.

L'IDE contiene un'icona Esegui Programma Basic nella barra degli strumenti, che esegue la prima macro presente nell'IDE. Fino a quando non la cambiate, la prima macro è quella vuota denominata Main. Modificate Main in maniera tale che appaia come mostrato nel Listato 2.

Listato 2: Modifica di Main per invocare CopiaNumAllaCol1.

Sub Main
  CopiaNumAllaCol1
End Sub

Adesso, potete eseguire CopiaNumAllaCol1 facendo clic ripetutamente sull'icona Esegui Programma Basic nella barra degli strumenti dell'IDE. Questo metodo è semplice e rapido, specialmente per macro temporanee che vengono utilizzate solo poche volte e poi cancellate.







A recorded macro repeats the same task over and over again. Before creating a recorded macro, I usually ask two questions:

  1. Can the task be summarized as a simple set of commands that do not change?
  2. Can the steps be arranged such that the last command leaves the cursor ready for the next command?

A complicated example

I frequently copy rows and columns of data from a web site and format them as a table in a text document. First, I copy the table from the web site to the clipboard. To avoid strange formatting and fonts, I paste the text into a Writer document as unformatted text. I reformat the text with tabs between columns so that I can use Table > Convert > Text to Table to convert to a table.

I inspect the text to see if I can record a macro to format the text (remember the two questions that I ask). As an example, I copied the FontWeight constants group from the OpenOffice.org web site. The first column indicates the constant name. Each name is followed by a space and a tab.

DONTKNOW The font weight is not specified/known.
THIN specifies a 50% font weight.
ULTRALIGHT    specifies a 60% font weight.
LIGHT specifies a 75% font weight.
SEMILIGHT specifies a 90% font weight.
NORMAL specifies a normal font weight.
SEMIBOLD specifies a 110% font weight.
BOLD specifies a 150% font weight.
ULTRABOLD specifies a 175% font weight.
BLACK specifies a 200% font weight.

I want the first column to contain the numeric value, the second column the name, and the third column the description. The desired work is easily accomplished for every row except for DONTKNOW and NORMAL, which do not contain a numeric value—but I know that the values are 0 and 100, so I will enter those manually.

The data can be cleaned in multiple ways—all of them easy. The first example uses keystrokes that assume the cursor is at the start of the line with the text THIN.

  1. Use Tools > Macros > Record Macro to start recording.
  2. Press Ctrl+Right Arrow to move the cursor to the start of “specifies".
  3. Press Backspace twice to remove the tab and the space.
  4. Press Tab to add the tab without the space after the constant name.
  5. Press Delete to delete the lower case s and then press S to add an upper case S.
  6. Press Ctrl+Right Arrow twice to move the cursor to the start of the number.
  7. Press Ctrl+Shift+Right Arrow to select and move the cursor before the % sign.
  8. Press Ctrl+C to copy the selected text to the clipboard.
  9. Press End to move the cursor to the end of the line.
  10. Press Backspace twice to remove the two trailing spaces.
  11. Press Home to move the cursor to the start of the line.
  12. Press Ctrl+V to paste the selected number to the start of the line.
  13. Pasting the value also pasted an extra space, so press Backspace to remove the extra space.
  14. Press Tab to insert a tab between the number and the name.
  15. Press Home to move to the start of the line.
  16. Press down arrow to move to the next line.
  17. Stop recording the macro and save the macro.

It takes much longer to read and write the steps than to record the macro. Work slowly and think about the steps as you do them. With practice this becomes second nature.

The generated macro has been modified to contain the step number in the comments to match the code to the step above.

Listing 2: Copy the numeric value to the start of the column.

 sub CopyNumToCol1
 rem ----------------------------------------------------------------------
 rem define variables
 dim document   as object
 dim dispatcher as object
 rem ----------------------------------------------------------------------
 rem get access to the document
 document   = ThisComponent.CurrentController.Frame
 dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 
 rem (2) Press Ctrl+Right Arrow to move the cursor to the start of "specifies".
 dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
 
 rem (3) Press Backspace twice to remove the tab and the space.
 dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 
 rem ----------------------------------------------------------------------
 dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 
 rem (4) Press Tab to add the tab without the space after the constant name.
 dim args4(0) as new com.sun.star.beans.PropertyValue
 args4(0).Name = "Text"
 args4(0).Value = CHR$(9)
 
 dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args4())
 
 rem (5) Press Delete to delete the lower case s ....
 dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())
 
 rem (5) ... and then press S to add an upper case S.
 dim args6(0) as new com.sun.star.beans.PropertyValue
 args6(0).Name = "Text"
 args6(0).Value = "S"
 
 dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args6())
 
 rem (6) Press Ctrl+Right Arrow twice to move the cursor to the number.
 dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
 
 rem ----------------------------------------------------------------------
 dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
 
 rem (7) Press Ctrl+Shift+Right Arrow to select the number.
 dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())
 
 rem (8) Press Ctrl+C to copy the selected text to the clipboard.
 dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
 
 rem (9) Press End to move the cursor to the end of the line.
 dispatcher.executeDispatch(document, ".uno:GoToEndOfLine", "", 0, Array())
 
 rem (10) Press Backspace twice to remove the two trailing spaces.
 dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 
 rem ----------------------------------------------------------------------
 dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 
 rem (11) Press Home to move the cursor to the start of the line.
 dispatcher.executeDispatch(document, ".uno:GoToStartOfLine", "", 0, Array())
 
 rem (12) Press Ctrl+V to paste the selected number to the start of the line.
 dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
 
 rem (13) Press Backspace to remove the extra space.
 dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
 
 rem (14) Press Tab to insert a tab between the number and the name.
 dim args17(0) as new com.sun.star.beans.PropertyValue
 args17(0).Name = "Text"
 args17(0).Value = CHR$(9)
 
 dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args17())
 
 rem (15) Press Home to move to the start of the line.
 dispatcher.executeDispatch(document, ".uno:GoToStartOfLine", "", 0, Array())
 
 rem (16) Press down arrow to move to the next line.
 dim args19(1) as new com.sun.star.beans.PropertyValue
 args19(0).Name = "Count"
 args19(0).Value = 1
 args19(1).Name = "Select"
 args19(1).Value = false
 
 dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args19())
 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 I should not have run the macro on the lines that did not have the proper format; I need to do these manually.

Running the macro quickly

It is tedious to repeatedly run the macro using Tools > Macros > Run Macro (see Figure 3). The macro can be run from the IDE. Use Tools > Macros > Organize Macros > OpenOffice.org Basic to open the Basic Macro dialog. Select your macro and click Edit to open the macro in the IDE.

The IDE has a Run Basic icon in the toolbar that runs the first macro in the IDE. Unless you change the first macro, it is the empty macro named Main. Modify Main so that it reads as shown in Listing 3.

Listing 3: Modify Main to call CopyNumToCol1.

 Sub Main
   CopyNumToCol1
 End Sub

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.

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