[Solved]I want to build a Specific Macro with Find-Replace

Particularly posted for Librebel with thanks for his intent to help: Yes, Hubert Lambert has solved this persistent problem at the Apache Open Office Forum: Solved: Match Individual Macro API with the API index?

I have this example_txt.odt to show how I wish to use “Line Breaks” and “Paragraph Marks” to ease and automate the numbering of writing point-wise instruction steps. If I used Para mark somewhat differently in the text, I would have to manually engage in editing the document to suit my purpose.

Please refer to these posts:

  1. Replace paragraph break with line break
  2. Keep what’s there without the non-printing chars

Recall the following:

Paragraph character (`¶`): U+000D CR Carriage Return (0x0013)
(entered at keyboard with `Enter` or `Return` key)
Line-Break character (`↲`): U+000A LF Line Feed (0x00010)
(entered at keyboard with `Shift+Enter` or `Shift+Return` key)

I want to build this macro:

  1. I will have two String variables declared within the macro.
  2. I use Find with Wild Characters to find out texts like [a-z.A-Z.0-9]$. (These are usually the Title for the Paragraph.)
  3. For the first string my find encounters, I take this entire string into the string variable, including the mark.
  4. I then count the length of the string. Suppose this is n.
  5. I now take (n-1)th ‘string-elements’ into consideration, and fill the second string with this truncated string and the as the last element.
  6. I replace the first string with this second string.
  7. I do this from the top every time, till there are no other such entries I wish to replace.

Presently, I am able to replace such strings like [a-z.A-Z.0-9]$ with #####&, the & code is used to print the ##### after the text but before the mark to help me identify such lines. These marks help me in manually editing the text.

How could I build this macro? I used the macro recorder, but can’t use the codes from within the macros for (): U+000D CR Carriage Return (0x0013) or (): U+000A LF Line Feed (0x00010)

Further Edited on 31/07/2017 at 1849Hrs (GMT+0530Hrs):
Dear librebel
One problem still remains:
I learnt to understand your codes. Thank you for introducing me to StarBasic. Subsequently, I am in https://forum.openoffice.org. I partially modified the strings to be replaced:

rem Replace all Paragraph Breaks With Linefeeds
Function Writer_ReplaceAll_Paragraph_Breaks_With_Linefeeds() As Long
REM Replaces all Paragraph breaks with a Linefeed character within the current Writer document.
REM Returns: the number of found/replaced occurrences.
Dim oReplace as Object
oReplace = ThisComponent.createReplaceDescriptor()
oReplace.setSearchString( "[a-z,A-Z,0-9]$" )
oReplace.setReplaceString( "&" + chr(10) )
oReplace.SearchRegularExpression = True
Writer_ReplaceAll_Paragraph_Breaks_With_Linefeeds = ThisComponent.replaceAll( oReplace )
End Function

Then I added another code to take care of the extra mark after the , because after running your modified code, I find that an extra empty para mark is left.

REM Reiteration to remove empty paras after above action
Function Writer_ReplaceAll_Blank_Paras() As Long
Dim bReplace as Object
bReplace = ThisComponent.createReplaceDescriptor()
bReplace.setSearchString( chr(10) + "^$" )
bReplace.setReplaceString( chr(10)  )
bReplace.SearchRegularExpression = True
Writer_ReplaceAll_Blank_Paras = ThisComponent.replaceAll( bReplace )
End Function

If I try to run this second code, I can’t find any result. Why? Where am I going wrong?

[Edited on 01/08/2017 in response to Librebel’s advice]
You said:

Perhaps you could use the following
macro for that: … …
oReplace.setSearchString( "$" )

But that line won’t serve my purpose. I am enclosing the three files, Test, Tested and WhatIWant

test.odt This is the original file to be edited.

tested.odt This is after running the first macro.

WhatIwant.odt This is what I really want.

I did try with \n\n in the second macro, but it doesn’t work. Where am I going wrong?

I would suggest you to dig into the OOoFBTools source code. That set of macros has a number of functions for various search and replace options involving paragraph and line breaks.

Thank you for the Russian Macros zipped link! I will go through the link. I only saw a huge list of macros provided and I am overwhelmed!
How to find reference for the Macro language, particularly, online reference tool like that in Eclipse IDE for Java like Javadoc, used in the LibreOffice Macro writing/viewing/editing?

A good place to start is Andrew Pitonyak’s OpenOffice.org Macro Information.

Hello @bkpsusmitaa,

Perhaps you could use the following macro for that:

Function Writer_ReplaceAll_Paragraph_Breaks_With_Linefeeds() As Long
REM	Replaces all Paragraph breaks with a Linefeed character within the current Writer document.
REM Returns: the number of found/replaced occurrences.
	Dim oReplace as Object
	oReplace = ThisComponent.createReplaceDescriptor()
	oReplace.setSearchString( "$" )
	oReplace.setReplaceString( chr(10) )
	oReplace.SearchRegularExpression = True
	Writer_ReplaceAll_Paragraph_Breaks_With_Linefeeds = ThisComponent.replaceAll( oReplace )
End Function

EDIT 2017-07-31

In your function Writer_ReplaceAll_Blank_Paras() it seems that the “^” doesn’t work properly…
try replacing with:

bReplace.setSearchString( "\n\n" )
bReplace.setReplaceString( chr(10) )

Oh, Great! I got the idea!
Now how do I use the part [a-z,A-Z,0-9]$ or [.]$ ?

you could set “[a-z,A-Z,0-9]$” as the Search string, but then it only replaces paragraph breaks that are directly preceded by an alphanumerical character, including that character. Please refer to this page for other valid expressions to enter as Search string.

Yes, that page is available from within my LibreOffice help too!

Because of space shortage, edited the main post with due attribution to you. Again, my thanks!

Because of space shortage, again edited the main post with due attribution to you. Again, my thanks!

In the interests of not duplicating effort, the original questioner here has posted a number of related questions on the AOO forum - see a list of the posts

Thanks for the outstanding detective work, Rob! Better than Doyle’s Sherlock! The contribution of Librebel was acknowledged in the very first post here: A Specific Macro with Find-Replace? None has yet solved the case. So duplication doesn’t yet occur. Everyone has been duly informed.

Librebel, there is a comment at the top of the introductory post! Thanks for your intent to help! Regards

You should accept the answer by clicking check mark just below voting handles. Placing [solved] in title is more suitable for classical forums, not QA sites. Thanks