[Solved] Simple macro replace string with incremental number

Creating a macro - Writing a Script - Using the API

[Solved] Simple macro replace string with incremental number

Postby vintvolt » Wed Jul 10, 2019 4:04 pm

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
vintvolt
 
Posts: 2
Joined: Wed Jul 10, 2019 3:48 pm

Re: Simple macro replace string with incremental number

Postby FJCC » Wed Jul 10, 2019 7:01 pm

Try this on a copy of your document
Code: Select all   Expand viewCollapse view
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
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7307
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Simple macro replace string with incremental number

Postby vintvolt » Thu Jul 11, 2019 11:46 am

Hi FJCC,
Thanks so much for the code! Works flawlessly.
Cheers
:super:
OpenOffice 4.1 on Mac / Latest on Windows 10
vintvolt
 
Posts: 2
Joined: Wed Jul 10, 2019 3:48 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests