[Solved] Simple macro replace string with incremental number

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
vintvolt
Posts: 2
Joined: Wed Jul 10, 2019 3:48 pm

[Solved] Simple macro replace string with incremental number

Post by vintvolt »

Hi all,
I've been struggling with programming a macro but I have been unable to do so, neither with recording nor programming from scratch.
It's very simple but I cannot seem to get my head around basic.
I have a document with a long text. Within this text there are many ** tags (two asterisks). I would need to replace all the ** tags with an incremental number:
Input:
** Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ** Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris ** nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor ** in reprehenderit in voluptate velit esse cillum dolore ** eu fugiat nulla pariatur.

Output:
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 2 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 3 nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor 4 in reprehenderit in voluptate velit esse cillum dolore 5 eu fugiat nulla pariatur.
Can somebody please guide me to a solution?
Thank you and kind regards
Last edited by robleyd on Thu Jul 11, 2019 11:58 am, edited 3 times in total.
Reason: Tagged [Solved]
OpenOffice 4.1 on Mac / Latest on Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Simple macro replace string with incremental number

Post by FJCC »

Try this on a copy of your document

Code: Select all

SDesc = ThisComponent.createSearchDescriptor()
SDesc.setSearchString("**")
Found = ThisComponent.findFirst(SDesc)
i = 1

While Not IsNull(Found)
	Found.String = i
	Found = ThisComponent.findNext(Found.End,SDesc)
	i = i + 1
WEnd
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
vintvolt
Posts: 2
Joined: Wed Jul 10, 2019 3:48 pm

Re: Simple macro replace string with incremental number

Post by vintvolt »

Hi FJCC,
Thanks so much for the code! Works flawlessly.
Cheers
:super:
OpenOffice 4.1 on Mac / Latest on Windows 10
Post Reply