# [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 C:\fakepath\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.

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 librebelOne 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 ... edit retag close merge delete ## Comments 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. ( 2017-07-28 09:16:47 +0100 )edit 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? ( 2017-07-28 19:28:44 +0100 )edit A good place to start is Andrew Pitonyak’s OpenOffice.org Macro Information. ( 2017-07-29 10:41:50 +0100 )edit ## 1 Answer Sort by » oldest newest most voted 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) )

more

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

( 2017-07-28 11:38:42 +0100 )edit

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.

( 2017-07-28 12:23:37 +0100 )edit

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

( 2017-07-28 18:32:27 +0100 )edit

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

( 2017-07-31 15:29:06 +0100 )edit

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

( 2017-08-01 09:55:25 +0100 )edit

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

( 2017-08-01 10:57:07 +0100 )edit

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.

( 2017-08-01 16:39:54 +0100 )edit

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

( 2017-08-27 08:31:14 +0100 )edit

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

( 2017-08-27 09:37:28 +0100 )edit