How to repeat a macro

I am using Version: 6.3.2.2 (x64) of Libre office. I recorded a macro to delete the “enter” at the end of a line. Now I would like to have this macro repeat until the end of the document. How do I do that? Here is the code:

sub DelEndSpace

rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object

rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:GoToEndOfLine", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Text" 
args2(0).Value = " "
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args2())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())

end sub

[Edit - Opaque] put code into preformatted text

The usage of the Macro Recorder is not the best way to create macros. The MR can work in the Witer and Calc applications only (can not work in draw, can not work in Impress), and it can not record all of the user’s activity.
.
Better to study and use the API functions of the LO. API: Application Programming Interface. Then you will able to WRITE your macros.
Start with Andrew Pitonyak’s free macro books.

There are several answers for your question.

The easiest one: assign a hotkey to your macro (for example, Shift+Delete) via the Tools - Customize - Keyboard menu. Now just press and hold these keys until you see your entire document turn into one long paragraph.

Moderately difficult option: rewrite your macro, but use not “jump to the end of the line and write a space”, but “search for the end of the line and replace with a space”. In this case, your code will immediately replace all line ends with spaces in one go, no loop is needed.

ReplaceAll.png

The most difficult way (for you now it is really difficult, perhaps someday later): I will not describe it, it has been described many times. This is best described in the book by Andrew Pitonyak (eg chapter 7.14.4. Search and Replace with Attributes and Regular Expressions)

An unbearably difficult decision: try searching this resource for the answer. If you are lucky, then very quickly you will find, for example, this question and the answers to it.

2 Likes

Sorry it has taken me so long to respond. I am not sure if I forgot to check for an answer or I simply did not receive yours in time. I appreciate your response and will try them some time and see if they work better than what I did.

For those who want to know. I made the macro in the picture above to simple delete the end spaces and needed to have it repeat. Here is the “not so great but it works” solution that I used.

  • Go to TOOLS - MACROS - EDIT MACROS

  • It will bring you to the Libre Office Basic screen where you can see the macro that you made. In my case the macro was named “DelEndSpace”. The first picture below shows the macro.

  • In the space above the DelEndSpace Macro, I wrote the following code. Note that code “For i = 1 To 800” equals the number of lines that I want it to check. I had to play around with the top number so that it included all lines but wasn’t too big that it went on endlessly.

I know that this is probably not the most efficient way to do this. I am a newbie when it comes to coding. But it worked.

In Response to Your Post, JohnSUN,

YOU: “rewrite your macro, but use not “jump to the end of the line and write a space”, but “search for the end of the line and replace with a space”. In this case, your code will immediately replace all line ends with spaces in one go, no loop is needed”

ME: I do not know how to rewrite the code as you have stated. I would need the exact coding.

ME: The find and replace that would have worked was the: Find $ and Replace (blank). With the key what you marked in the red rectangle – to check “Regular Expressions.”

YOU: An unbearably difficult decision: try searching this resource for the answer. If you are lucky, then very quickly you will find, for example, this question 4 and the answers to it.

ME: I take as a criticism. I always search for answers to my questions for far too long most times before posting. The post described what you already shared about the find and replace options.

ME: While I realize that the Find and Replace options are ways of getting rid of the end spaces, however, it does not address my initial question which was: Now I would like to have this macro repeat until the end of the document. How do I do that?

While I appreciate the help, I did not appreciate the attitude. Blessings!

I thought JohnSUNs attitude was exemplary, two good pointers and reference to two good sources of information. It’s not his fault you didn’t look at his reply.

Most people wouldn’t create a macro for something that can be done with one Find and Replace. There must be something more behind your question that you require a macro instead, possibly further automation? In which case the pointer to the macro book would be helpful

1 Like

To be fair, it wasn’t very long: this resource knows discussions that, for example, started on Jun 2, '15 and were closed on May 21, '21

If a bad solution works (performs a task), then it is not such a bad solution.

This assumes that you use the same sequence of steps as you did to record your original DelEndSpace macro: choose Tools - Macros - Record Macro, choose Edit - Find & Replace (or press Ctrl+H), set the find and replace options as appropriate to your needs (you wrote that Find $ and Replace (blank) worked for you), click “Replace All”, stop recording the macro, and save the resulting code as a new procedure. The result should have been something similar to this (some lines are skipped and replaced with ellipsis for brevity):

sub DelEnter
rem define variables
dim document   as object
dim dispatcher as object
...
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
...
dim args1(21) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
...
args1(11).Name = "SearchItem.SearchString"
args1(11).Value = "$"
args1(12).Name = "SearchItem.ReplaceString"
args1(12).Value = " "
...
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())
end sub

This procedure would actually do the job without you explicitly specifying a cycle on your part (the cycle is there, but it is not visible in the procedure code, it is hidden inside the .uno:ExecuteSearch command)

No, this is not criticism or sarcasm - it was written quite sincerely and means exactly what is written: searching this resource is quite difficult even for veterans, not to mention newcomers. As far as I remember, I spent about an hour to find a link to said discussion for you. However, with a certain amount of luck, the right answer can be found almost instantly.

I assure you, if I wanted to be rude, I would find an easier way than listing different solutions - for example, I would just write

RTFM

(By the way, this short answer would lead you to your decision much faster)

I assure you - today it is not my biggest problem. Blessings!

2 Likes

My apologies to you for taking your response as criticism. You have been very helpful. I too have spent much time looking through this forum for solutions and thought that you were being critical. my bad. By the way, I am not a newbie to the forum only to coding. Hats off to you for being kind and to the point, JohnSUN