Multi-search and replace macro


I want to create a macro to replace a bunch of words with something else, and then revert everything back. A kind of dictionary macro, if you will.

For example, I would run the macro with these entries:

multiplication → multi1
defibrillation → defib1
utilization → util1

then the reverse would search for the items on the right side and revert them back to the left side.

The use case is to determine the Flesh-Kincaid readability score of text correctly, without letting jargon get in the way.

One thing that’s important is this: if the search term doesn’t exist, it should not make the macro fail. The macro should move on to the next term.

Does anyone know of a macro that already exists (or code that already exists) that does something similar? I can take it and adapt it.

Or, if someone finds this a fun pastime and wants to write it and post it here, knock yourself out! :slight_smile:



Since the problem has nothing to do with text formatting, I’d export it to plain text. Then, I’d used a macro generator to make the replacement through a dictionary (I’ve already written such a utility to “upgrade” spelling to a modern version of a language (XIX century Norwegian to bokmål)).

But transforming the text is only one part of the story. Once it is transformed, what do you do with it? In other words, what are the subsequent steps?

PS: general information on F-K readability tests here.
If you know how to define a syllable, the macro generator can altogether output the F-K score. Since a syllable is a phonetic unit, would counting adjacent vowels be sufficient in English (assimilating diphthongs and triphthongs to a single syllable)?

1 Like


One of the problems of FK scores is the length of jargon words. I already have a tool that gives me the score, but it’s always very high because of technical terms. Some of those terms have to be used, you don’t get around it.

So this is what I want to do:

  • Find all long jargon words
  • Replace by shorter words, even if they are nonsensical
  • Check the FK score
  • Revert to technical terms (so undo the search and replace)
  • Fix the problematic sentences
  • Repeat

I would prefer to do it all within the LO interface instead of having to export to text.



I have never yet written any LO macros. However I have the feeling they won’t be the adequate tool for that because you need to parse your text. Regular expressions are not powerful enough and even if you were able to write the extremely contorted regexp to recognise your dictionary, you won’t be able to do it in one pass. To add to the difficulty, you may have to manage formatting information interspersed in the text.

Since your task acts on contents (not on appearance or formatting), I think working in simple text editor is not a hindrance. When your text is fixed, you just import into Writer and apply styles (that’s the “professional” and very comfortable way to format documents).

So your task is just to save periodically your plain text, launch the macro generator, read the F-K score and make correction in the text editor. As I imagine processing, there is no need to actually save the replacement. When text is parsed, words are internally substituted (but not saved, thus preserving text integrity) and score computed, all in the same single pass.

You have two windows open on your screen: text editor and console. You save in the text editor, you switch to the console to repeat the command, read the result and switch back to text editor. Quite simple.

By the way, which is your OS?

I’m on Windows.

I’m curious, does searching in a macro only work with regular expressions and formatting?

I was hopping I could build an array of strings and loop through them. That’s the reason I want a macro: to avoid having to do 20 search and replace combinations manually.

Hmmm… I’ll have to think about a better option, then.


@lduperval No, no, you misunderstood. This can indeed be done with a macro (see the listings in Chapter 7.14. Search And Replace - this may be sufficient for your purposes). We just wanted to make your task easier and tried to find alternative ways to solve it.

(Sorry, but we are already used to people asking about solving particular problems, although they could get help with solving the whole task)

Bad luck! I have a utility under Linux which could have done the job very easily (and the challenge interested me).

Is it possible to combine searched words into one string? To use this string for regexp finding:
Or you use in searched words also some characters that are control characters in regexp expressions like . + etc.?

With only alphanumeric characters in searched words it is possible to modify macro from example (ODT file)

→ it will find some text, set the Property CharFlash, and next searching is only for CharFlash words.
So it is possible to change it → set CharFlash to primal words, do Replace, find CharFlashed words and do reverse Replace.

But if you use formatted text, then formatting mostly will be lost like in classical Find&Replace

To be honest, I do not understand the need for a reverse replacement. How about creating a copy of the document, doing all the replacements there, and analyzing it without touching the original text?

1 Like

That works, but I still need the replacement macro soi can search and replace multiple words. To me, it seems like the reverse is just running the same macro with a different order.

So something like:

  • Setup technical terms array
  • Setup shorter terms array
  • Call multi-search-replace(fromArray, toArray)
  • Do my checks
  • Call multi-search-replace(toArray, fromArray)

And in the function, something like

For (I=0; i<array.length; i++) {
SearchReplace(searchArray(i), replaceArray(i));

I’ll take a look at the examples you mention above and see how I can adapt it. I’m more of a Java, C, and Tcl guy than a BASIC guy, so these macros kinda make my head spin.


I had head-aches from C++ about 20 years back :-), but sometimes I have “some inconvenience in head” when I see a Java code :slight_smile:.

I did it for array-pairs in an Array(), I suppose it isn’t problem for you to change it to 2 arrays if needed. Run macro ReplaceNormal, then macro ReplaceReverse

Sub ReplaceNormal
End Sub

Sub ReplaceReverse
End Sub

Sub multiReplace(optional bReverse as boolean) 'find&replace according to pairs in array p() 
		'bReverse=false -> Find: 1st item, Replace: 2nd one; bReverse=true -> Find: 2nd, Replace: 1st
	if isMissing(bReverse) then bReverse=false
	dim oDoc as object, p(), i&, oDesc as object
	p=array( array("multiplication", "multi1"), array("defibrillation", "defib1"), 	array("utilization", "util1") )
	oDesc=	oDoc.createReplaceDescriptor()
	with oDesc
		.SearchCaseSensitive=true 'case sensitive
		.SearchRegularExpression=false 'no regexp
	end with
	for i=lbound(p) to ubound(p)
		with oDesc
			if bReverse then
			end if
		end with
	next i
End Sub

If you will put 20 pairs to Array p(), then macro will do 20x replaceAll in the document. But if is it possible for you to make one string for regexp I asked you (multiplication|defibrillation|utilization), then it is possible to use method .findNext instead .replaceAll and it will do replacements in one iteration of the document. Maybe it will be faster, maybe no, I don’t know now.

1 Like

Thanks for this. I will test it to see if it fits the bill. I will also see if I can use a regexp like you mention. I’ve never tried that particular approach before. As you said, it could be much faster.



Example for things I wrote, more likely for interest → one regexp string (word1|word2), and property CharFlash (now also to keep up the formatting). But when I tested it for 20 pages with formatted text the Libre crashed :-(.
example-for-formatted-text.odt (69.1 kB)