Collapse two lines into one

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Collapse two lines into one

Post by SmallFry »

As I do some clean-up to some documents, one of the sections that starts with A4 has some paragraphs that got separated.

From the example below, there are four paragraphs, but the second paragraph has been separated to the next line making five lines. How can I get the second paragraph to be all in one line?

Before
A4.1. A glittering gem is not enough.
A4.2. I was very proud of my nickname throughout high school, but today
I couldn’t be any different to what my nickname was.
A4.2.1. The quick brown fox jumps over the lazy dog.
A4.2.2. The waves were crashing on the shore; it was a lovely sight.

After
A4.1. A glittering gem is not enough.
A4.2. I was very proud of my nickname throughout high school, but today I couldn’t be any different to what my nickname was.
A4.2.1. The quick brown fox jumps over the lazy dog.
A4.2.2. The waves were crashing on the shore; it was a lovely sight.

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Collapse two lines into one

Post by HansV »

Does this macro do what you want? (Select the text before running it)

Code: Select all

Sub MergeParagraphs()
    Dim i As Long
    Dim n As Long
    Application.ScreenUpdating = False
    For i = Selection.Paragraphs.Count To 1 Step -1
        If Left(Selection.Paragraphs(i).Range.Text, 3) <> "A4." Then
            n = Selection.Paragraphs(i).Range.Start
            ActiveDocument.Range(n - 1, n).Text = " "
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Collapse two lines into one

Post by SmallFry »

Hi Hans,

Yes, this works perfectly.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15585
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Collapse two lines into one

Post by ChrisGreaves »

Hi SmallFry
Pleas see also the macro "GlueLetters" in the attached text, part of my "TextChange" module, various options to glue lines (or paragraphs) together.
Cheers
Chris
You do not have the required permissions to view the files attached to this post.
There's nothing heavier than an empty water bottle

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Collapse two lines into one

Post by SmallFry »

Hi Chris and thank you for the reply,

I copied out this text file code, but it seems to be missing something.

I see as I run it, it returns a debug to the line

Code: Select all

Set rng = rngGetSel(Selection)
Sub or Function not defined

I suppose this could cause a problem also!

Code: Select all

Call ClearFindReplaceFormatting(Selection)

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Collapse two lines into one

Post by HansV »

Chris has a habit of posting code that relies on his extensive library of utilities, without posting the necessary utilities...
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15585
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Collapse two lines into one

Post by ChrisGreaves »

SmallFry wrote:Set rng = rngGetSel(Selection)
Please accept my apologies. I posted the code to give you an idea, from a working function, or what might be done.

I have attached a copy of the rngGetSel function from my library UW.DOT. Drag the commented TEST macro outside the function, decomment, and run the TEST after selecting a chunk of text in an active document. The TEST macro should MsgBox the selected text.

You can download a copy of UW.dot in the Indxr package whose link(s) are provided in this post. If the DOWNLOAD fails PM me with an email address and I will attach a copy of UW.dot to a reply by email.

If you want to explore more line-joining code I have attached a module from my Under.dot, although some of those procedures too might fail to execute without UW.dot.

Cheers
Chris
You do not have the required permissions to view the files attached to this post.
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15585
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Collapse two lines into one

Post by ChrisGreaves »

HansV wrote:Chris has a habit of posting code that relies on his extensive library of utilities, without posting the necessary utilities...
True. Partly because of the Lounge's 256Kb limit, but also because I figured I had posted enough copies of UW.dot to last a lifetime!

Well, also because I am split between providing an immediate fix, and allowing someone who is learning time to learen.

Time for another poll, I think
Cheers
Chris
There's nothing heavier than an empty water bottle

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Collapse two lines into one

Post by SmallFry »

Well, also because I am split between providing an immediate fix, and allowing someone who is learning time to learn.
And truly appreciated Chris. I will do my best to learn from what you have provided.