Page 1 of 2

[Solved Elsewhere] A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 5:51 am
by rajibando
[Solved at the link posted in the last post of this thread]
I am new to LibreOffice / OpenOffice Macro programming.
I posted the following thread to the LibreOffice forum:
I want to build a Specific Macro with Find-Replace
I received a partial response with codes, and also guidance to reach and download from OOoFBTools with a lot of codes, which I can't yet properly discern as I have just begun reading the APIs.
I want to build this macro:
  • I will have two String variables declared within the macro.
  • 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.)
  • For the first string my find encounters, I take this entire string into the string variable, including the ¶ mark.
  • I then count the length of the string. Suppose this is n.
  • 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.
  • I replace the first string with this second string.
  • I do this from the top every time, till there are no other such entries I wish to replace.
Could I be helped with the Macro creation?

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 7:04 am
by robleyd
You may find an issue with replacing ¶ (paragraph marker) with ↲ (new line) as AOO, and possibly LO, has a 64K limit on the size of a paragraph; paragraphs as you doubtless know are delimited by the ¶

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 7:14 am
by Zizi64
I want to build this macro:

I will have two String variables declared within the macro.
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.)
For the first string my find encounters, I take this entire string into the string variable, including the ¶ mark.
I then count the length of the string. Suppose this is n.
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.
I replace the first string with this second string.
I do this from the top every time, till there are no other such entries I wish to replace.
Why you need a new macro for this task? It seems a normal "Find and replace" procedure. (Maybe you need install the Alternative F&R extension. As I remember, that extension can seach for the ¶ mark and can replace it with ↲ mark.)

Be careful with the changing ¶ marks to ↲ marks. (see robleyd's post).

https://extensions.openoffice.org/en/pr ... -altsearch

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 8:02 am
by rajibando
Thank you, Zizi64 and robleyd for posting so quickly!
I need the macro because I wish to (1) replace the ¶ with ↲ for strings satisfying `[a-z,A-Z,0-9]¶` and (2) keep ¶ for strings satisfying `[.]¶`

Had you kindly perused the Ask.LibreOffice link I had provided with my thread, you would see that I need to add "&"+"Ch(10)" and then remove the empty paragraphs with find "^$" with the complete code provided.
But the problem is: if I use the macro code, then the Find "^$" no longer works! Says, "Search key not found."
test.odt
You could test with this file
(11.27 KiB) Downloaded 332 times
Code Courtesy: Librebel

Code: Select all

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

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 8:20 am
by Zizi64
Please upload your sample ODF type Writer file with the embedded macros (with all of the related macro functions and procedures).

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 8:31 am
by rajibando
Posted above in the edited post while you were typing! Sorry! How does one embed macros with a file?

BTW, Alternative F&R extension is complicated, without visual examples.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 9:47 am
by Zizi64
Posted above in the edited post while you were typing!
OK thanks, I see the attachment now.


Sorry! How does one embed macros with a file?
Copy the macro code into a Module of the Standard Library of the document. (You need create a Module before)


BTW, Alternative F&R extension is complicated, without visual examples.
Alt_F&R.png

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 9:54 am
by RoryOF
AltSearch help file is at
http://mujweb.cz/macrojtb/HelpAltSearch ... 1501314727

Learn how to use AltSearch rather than reinvent the wheel.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 9:58 am
by Zizi64
Alt_F&R_2.png

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 10:10 am
by robleyd
AltSearch help is also available from the question mark ? in the Altsearch window.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 10:15 am
by Zizi64
Note:
Replacing ¶ (paragraph marker) with ↲ (new line) will convert your original Style-formatted text into ONE paragraph wich will have ONE paragraph style.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 10:40 am
by rajibando
RoryOF wrote: ... Learn how to use AltSearch rather than reinvent the wheel.
Very clumsily written! I had seen it earlier, but chose to avoid it.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 10:44 am
by rajibando
Zizi64 wrote:Note:
Replacing ¶ (paragraph marker) with ↲ (new line) will convert your original Style-formatted text into ONE paragraph wich will have ONE paragraph style.
That was only a sample file for illustration!
And thanks for the snapshot!
I am irritated at the very thought of negotiating that clumsy help link of Alt-Search! Only if the creator could write a reader-friendly help file!

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 10:51 am
by Zizi64
I am irritated at the very thought of negotiating that clumsy help link of Alt-Search! Only if the creator could write a reader-friendly help file!
The extensions usually are third-pary softwares. Please contact with the distributor.
http://mujweb.cz/macrojtb/HelpAltSearch_en.html
And the extensions usually are open source softwares. You can modify them as you want it.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 10:54 am
by rajibando
BTW, how did you Find-Replace for that file I uploaded? Thank you in advance!
Zizi64 wrote:The extensions usually are third-pary softwares. Please contact with the distributor.
Yes, I am aware of it!
Zizi64 wrote:And the extensions usually are open source softwares. You can modify them as you want it.
A considerable challenge! 1st, to understand the methods by reading the usage, then editing and modifying the content!
Moreover, the programmer doesn't even have a contact email-Id.!

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 11:06 am
by Zizi64
A considerable challenge! 1st, to understand the methods by reading the usage, then editing and modifying the content!
You wrote:
I want to build a Specific Macro with Find-Replace
I supposed you have some knowledge to create (write) macros...

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 11:09 am
by Zizi64
BTW, how did you Find-Replace for that file I uploaded? Thank you in advance!
I just used the AltSearch extension as the attached pictures show you... Do not forget to check in the "Regular expressions" option.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 11:17 am
by rajibando
Zizi64 wrote:... I just used the AltSearch extension as the attached pictures show you...
I am so sorry! I only checked from the thumbnail image of this forum that ¶ (paragraph mark) was replaced by ↲ (new line) and so I knew one could do it with alt-search if one wanted to.
BTW, do you have the contact email-id of the developer? The page doesn't appear to have that!

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 11:18 am
by RoryOF
rajibando wrote: I am irritated at the very thought of negotiating that clumsy help link of Alt-Search! Only if the creator could write a reader-friendly help file!
From memory, the author of the AltSearch extension is not a native English speaker. If you don't like it, don't use it; write something better if you wish.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 11:26 am
by robleyd
There is a link to the author both on the online help at the link Zizi64 posted, and also from the same help page available locally through the help on the Altsearch window. To save you looking it up, the link is http://mujweb.cz/macrojtb/?redirected=1501320128; it appears to be in the Czech language but there is an email address.

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 12:28 pm
by rajibando
robleyd wrote:There is a link to the author both on the online help at the link Zizi64 posted, and also from the same help page available locally through the help on the Altsearch window. To save you looking it up, the link is http://mujweb.cz/macrojtb/?redirected=1501320128; it appears to be in the Czech language but there is an email address.
And I thank you for your kindness!
I found the email-id once you said there was one.
My script blocker had blocked the javascript in the original Czsech page.
The code was here:

Code: Select all

<p align="RIGHT">Tomáš Bílek -  <script language="JavaScript">mkemail('macrojtb','volny.cz','?subject=cs - ');</script></p></div>

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 3:48 pm
by rajibando
@Zizi64:
You used only `$` in the `find` textbox, while I used `[a-z,A-Z,0-9]$` and replaced those para with `\n`
After I have replaced the ¶ (paragraph marker) with ↲ (new line) with Alt-Search, I find that ↲ is immediately followed by an empty ¶.
Strangely, if I want now to replace the created `^$` with no character, Alt-Search can't find any empty paragraph!
I am attaching the modified file here:
test.odt
The modified Test file
(11.3 KiB) Downloaded 326 times
Maybe my version of LibreOffice has a bug. The version of AlternateSearching I used is 1.4.2 of 3/2017, file size 562.1 KiB (575,605 bytes)

[Later]
No, please check your snapshot with my requirement. Only the lines with `[a-z,A-Z,0-9]$` had to have the `$` (¶ i.e., paragraph marker) replaced by a ↲. Also, your first line and second line got joined!

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 4:26 pm
by Zizi64
Maybe I not understood your task exactly. Please upload your document with BEFORE and AFTER state text. (make a copy of the text and modify it manually.)

Re: A Specific Macro with Find-Replace?

Posted: Sat Jul 29, 2017 6:11 pm
by rajibando
The earlier file I uploaded first.
The work I did:
to (1) replace the ¶ with ↲ for strings satisfying `[a-z,A-Z,0-9]$` and to (2) keep ¶ for strings satisfying `[.]¶`
The amended file after Alt-Search after find-replace operation, here.

After I have replaced the ¶ (paragraph marker) with ↲ (new line) with Alt-Search, I find that ↲ is immediately followed by an empty ¶. I also find that the character immediately preceding ¶ is also deleted.

But if I used `&\n` in Alt-search, the Replace option gives the following error message:

Code: Select all

BASIC runtime error.
An exception occurred 
Type: com.sun.star.resource.MissingResourceException
Message: StringResourceImpl: No entry for ResourceID: \n.
and halts at the macro code `seznam.addItem(neco,0)` of `sub pridejDoSeznamuA(seznam, neco, promene, pevne)`

Strangely, if I want now to replace the created `^$` with no character in the `Replace` box,, Alt-Search can't find any empty paragraph!

In normal LibreOffice Find-Replace, I would have needed `&\n` but it would again be an addition of `$` (i.e., visually, ¶ addition).

Only the macro-code I was provided with does some justice but with an addition of `$` (i.e., visually, ¶ addition)!

What I want is this file:
WhatIwant.odt
(11.31 KiB) Downloaded 300 times
If at all possible, will this indicated alteration to the original codes I was given, do?:

Code: Select all

rem *****************************************
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 )
REM Reiteration to remove empty paras after above action
     Dim oDReplace as Object
    oDReplace = ThisComponent.createReplaceDescriptor()
    oDReplace.setSearchString( "^$" )
    oDReplace.setReplaceString( "" )
    oDReplace.SearchRegularExpression = True
    Writer_ReplaceAll_Paragraph_Breaks_With_Linefeeds = ThisComponent.replaceAll( oDReplace )
End Function

Re: A Specific Macro with Find-Replace?

Posted: Mon Jul 31, 2017 9:17 am
by rajibando
Okay, read and found a solution! But help me make it run as intended.
Created two functions and ran them, one after the other.

Code: Select all

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
&

Code: Select all

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
Then I run the first, then the second, The second one is not running as intended.
I have to have your help to catch the error.
Regards

Re: A Specific Macro with Find-Replace?

Posted: Mon Jul 31, 2017 9:50 pm
by Zizi64

Code: Select all

oReplace.setSearchString( "[a-z,A-Z,0-9]$" )
Do you want replace the "Paragraph End" mark to LineFeed mark, when the last letter of the paragraph IS a letter of the array A-Z OR a-z OR a 0-9 numeric character or a COMMA?
But you want not replace it, when the last letter of the paragraph IS a space OR dot OR exclamination mark OR a question mark OR all of other special characters?
Your first macro can to do this.

Re: A Specific Macro with Find-Replace?

Posted: Mon Jul 31, 2017 10:05 pm
by Zizi64
Fhjb iui ugigh ugg↲
The pilcrow in the second line of the attached rext IS NOT an empty paragraph, because the LineFeed mark IS NOT a paragraph end mark. Therefore the all of the characters and marks in this two lines mean ONE paragraph only. You will not recognise the pilcrow (¶) as an empty paragraph, because that is not an empty paragraph, but that is the END MARK of the only one paragraph.

Re: A Specific Macro with Find-Replace?

Posted: Tue Aug 01, 2017 4:05 am
by rajibando
Zizi64 wrote:

Code: Select all

oReplace.setSearchString( "[a-z,A-Z,0-9]$" )
Do you want replace the "Paragraph End" mark to LineFeed mark, when the last letter of the paragraph IS a letter of the array A-Z OR a-z OR a 0-9 numeric character or a COMMA?
But you want not replace it, when the last letter of the paragraph IS a space OR dot OR exclamination mark OR a question mark OR all of other special characters?
Your first macro can to do this.
Yes, but how? 'Comma', 'space', 'exclamation mark', 'question mark' I can add later with an escape char `\` before the marks.

Re: A Specific Macro with Find-Replace?

Posted: Tue Aug 01, 2017 7:01 am
by Zizi64
Yes, but how? 'Comma', 'space', 'exclamation mark', 'question mark' I can add later with an escape char `\` before the marks.
If you will replace ALL of the paragraph marks from your document, you will be in truble. (Please read robleyd's first answer.)
In that case your text wil be tranfromed into only one pararaph, and that only one paragraph (maybe) will reach the limit of the charater numbers in a paragraph.

Re: A Specific Macro with Find-Replace?

Posted: Tue Aug 01, 2017 7:14 am
by rajibando
Zizi64 wrote:If you will replace ALL of the paragraph marks from your document...
Just changing from this to this
Just changing from this to this
If you look at the code, you will notice that I am not removing all paragraph marks from my document.
So, please ...