[Tutorial] How to record a macro (and Regular Expressions)

Forum rules
No question in this section please
For any question related to a topic, create a new thread in the relevant section.
Post Reply
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

[Tutorial] How to record a macro (and Regular Expressions)

Post by John_Ha »

Recording a macro to do something repetitive is very easy. In this example, we will record a macro to do Find and Replace in Writer, though you can also record macros in other AOO programs like Calc. It is based on this post: [Solved] Find and replace (advanced for me)

We will use Regular Expressions in the Find and Replace search, but of course you can record a macro to do (virtually) anything you can do in Writer.

You can find information on Regular Expressions in:

- Chapter 3 of the OOo v3.3 Writer Manual
- Documentation/How Tos/Regular Expressions in Writer and
- Documentation/How Tos/Regular Expressions in Calc.

Also, when the Find and Replace pop-up window is open, click on Help and then click on List of Regular Expressions for a list of all the available Regular Expressions and links to pages with more detailed explanations. Search the forum with regular expressions for many hits. Regular expressions are common across many applications so Google for more information where you will find sites like Regular-Expressions.info.

The Alternative Find and Replace add-on is excellent for using regular expressions - you select from a menu of what you want.

The book Mastering Regular Expressions by Jeffrey E. F. Friedl is widely regarded as an excellent and very comprehensive book. You can download Chapter 1 free from here.

See the following text

*** SHOW DOWN ***
Player3: shows [Ts Kh] (a pair of Sixes)
Player1: shows [Kd 6s] (three of a kind, Sixes)
Player1 collected $14.90 from pot

where I want to delete the entire blue contents inside Player3's square brackets, replace it with five spaces, and delete the rest of the line:

So, I want to replace "Player3: shows [Ts Kh] (a pair of Sixes)" with "Player3: shows [.....]"

To do this manually, you need to use Regular Expressions in Find and Replace to find a "thing you define" and work with it. So
1 Edit > Find and Replace > More > tick Regular Expressions

2 Type the following in the Find and in the Replace boxes:

FIND (Player3: shows \[)(.. ..)(\])(.*)

REPLACE $1*****$3 Note: ***** should be typed as 5 spaces - the forum deletes redundant spaces so I cannot show them properly

3 Find > Replace All

How does it work?

The () brackets separate what is found into four separate variables, which are referred to as $1, $2 (not used),$3 and $4 (not used) in the Replace box.

(Player3: shows \[) The $1 variable. \[ is necessary because the [ is a special character - the preceding \ identifies it as a "normal character to be found", not a Regular Expression Special Character

(.. ..) The $2 variable. This is any two characters (the two dots), followed by a space, followed by any two characters (the two dots)

(\]) The $3 variable. This is the character " ] "

(.*) The $4 variable. This is any number of any characters

In the REPLACE box, we write back variable $1, followed by 5 spaces, followed by variable $3 (which is easier than wondering if " ] " is a special character in Replace!) We do not write back $2 or $4 as we want to get rid of them.

When testing, build up the FIND expression by small steps, starting very simply and just try to find it. I started with "Player3: shows" which, of course, it found. I then added the " [ " and it did not find it - hence I knew adding the " [ " broke the search. A quick check in Help showed that I needed " \[ " not " [ ". I then added the brackets and it still worked. I then added (.. ..) etc until I had built up the entire expression.
We now record a macro to do it.
1 Tools > Macros > Record macro. This brings up a little Stop Recording pop-up window

2 Edit > Find and Replace ... and do the search you require. Click Replace All, then exit

3 [Optional step ... in the regular expressions example, the tick in regular expressions will stay there and may confuse your next search. To get rid of it, untick it, and then do a dummy search for anything (qwert), and then click Ignore all. Exit.]

4 Press Stop Recording, and save the macro in My Macros > Standard > Module 1 ..., (no spaces in its name)

5 Run the macro by Tools > Macros > Run macro..., and navigate to the macro. You can optionally put the macro name on a Toolbar by r-click the toolbar > Configure ..., and add the macro. You can choose an icon for it if you want, or draw one (16 x 16 pixels BMP file) and use it.
All done.

See also the up to date Writer guide and/or Chapter 16 Customizing Writer in the OOo v3.3 Writer Manual for more information on macros. Also see Chapter 13 - Getting Started With Macros in the Getting Started Guide - V3.3.

If you want to edit or write, as opposed to record, a macro, Andrew Pitonyak's OpenOffice Macro Information is a gold standard reference for OpenOffice macros. Also visit the Macros and UNO API forum for assistance.
List of regular expressions
List of regular expressions
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
Post Reply