[Solved] Macro to create 1-sentence paragraphs

Discuss the word processor
Post Reply
Last One
Posts: 3
Joined: Mon Mar 22, 2021 6:18 pm

[Solved] Macro to create 1-sentence paragraphs

Post by Last One »

Hi
I am looking for a macro to find the end of a sentence in the text and move it to the beginning of a new line. Modify all text to form one sentence one line.
I was looking for but did not find ..
Last edited by MrProgrammer on Wed Mar 31, 2021 6:37 pm, edited 2 times in total.
Reason: Tagged ✓ [Solved]
Open Office 4.1.0 / LibreOffice 7.1 on Windows 10 Home
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro in Writer

Post by RoryOF »

Why use a macro? This can be done with three or four passes of Find and Replace - one if you use a Regular expression.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Last One
Posts: 3
Joined: Mon Mar 22, 2021 6:18 pm

Re: Macro in Writer

Post by Last One »

RoryOF wrote:Why use a macro? This can be done with three or four passes of Find and Replace - one if you use a Regular expression.
Hi

It needs to be automated. I have, for example, 200 text, one click and change of the entire document.

***************************
I wanted to traverse each paragraph and possible change the paragraph styles. To do this, I needed to select one paragraph at a time. My first attempt, which was wrong, involved a starting point of having a paragraph selected. I then moved right zero spaces to remove the selection from the current paragraph.

macro

I wanted to traverse each paragraph and possible change the paragraph styles.
To do this, I needed to select one paragraph at a time.
My first attempt, which was wrong, involved a starting point of having a paragraph selected.
I then moved right zero spaces to remove the selection from the current paragraph.
Open Office 4.1.0 / LibreOffice 7.1 on Windows 10 Home
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro in Writer

Post by RoryOF »

Please define "end of a sentence".
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Last One
Posts: 3
Joined: Mon Mar 22, 2021 6:18 pm

Re: Macro in Writer

Post by Last One »

RoryOF wrote:Please define "end of a sentence".
End of sentence. < dot
Open Office 4.1.0 / LibreOffice 7.1 on Windows 10 Home
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro to create 1-sentence paragraphs

Post by RoryOF »

What about ? ! " '

You have to think this out.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Macro to create 1-sentence paragraphs

Post by Villeroy »

menu:Edit>Find&Replace....
[X] Regular expresssion
Find: ([.?!])[:space:]*
Replace: $1\n
[Replace All]
replaces points, ? and ! followed by optional space with point, ? or ! and a paragraph break.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to create 1-sentence paragraphs

Post by JeJe »

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
MrProgrammer
Moderator
Posts: 4908
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Macro to create 1-sentence paragraphs

Post by MrProgrammer »

Hi, and welcome to the forum.
Last One wrote:… find the end of a sentence in the text and move it to the beginning of a new line.
Last One wrote:It needs to be automated.
Last One wrote:End of sentence. < dot
Record a macro. You do this once as described in Help → Index → recording;macros. You don't need to know any programming but you do need to learn about how to record a macro. Then record this one action.

• Edit → Find & Replace → More options, ✓ Regular Expressions, Search for \.\s+, Replace with .\n, Replace All.

Your macro is very simple because it only requires one step. Call it (Tools → Macros → Run macro) whenever you need it. Or use a keyboard shortcut, menu, or toolbar to run it.
How can I record a macro?
[Tutorial] Binding a macro: Shortcut key, menu or toolbar

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the subject field. Select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to create 1-sentence paragraphs

Post by JeJe »

Something like this for the textcursor method. (test on a copy of your document with just the text you want converted in it.)

Code: Select all

Sub ConvertToSentences()
txt = thiscomponent.text
tc= txt.createtextcursorbyrange(txt.start)
do 
ret = tc.gotoEndOfSentence(false)
if ret = 0 then exit do
tc.goright(1,true)
if tc.string=" " then 

tc.string  =""
else
if tc.string <> chr(10) then tc.Text.insertControlCharacter(tc,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, True)
end if
tc.collapsetoend
tc.goright(1,false)
loop

End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply