Fix broken sentences

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Fix broken sentences

Post by gailb »

As you can see from the picture, I have a document with broken sentences. In #1, the 13 should not be on its own line, along with #2, Nehemiah should not be on its own line. How can this be fixed?

Before
Screenshot.png
After
Screenshot2.png

I. Introduction
A. Grow and become strong in your faith
1. That's why we go through the Bible "precept upon precept, line upon line" (see Isaiah 28:10,
13)
2. Nehemiah and Ezra read from the law and then gave the sense (see
Nehemiah 8:8)
3. Verse by verse, chapter by chapter
You do not have the required permissions to view the files attached to this post.

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

Re: Fix broken sentences

Post by HansV »

Try this macro. Test it on a copy of the document, please!

Code: Select all

Sub FixSentences()
    Dim i As Long
    Dim rng As Range
    Application.ScreenUpdating = False
    For i = ActiveDocument.Paragraphs.Count To 2 Step -1
        Set rng = ActiveDocument.Paragraphs(i).Range
        If Not rng.Text Like "?. *" Then
            rng.Collapse
            rng.MoveStart Unit:=wdCharacter, Count:=-1
            rng.Text = " "
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Fix broken sentences

Post by gailb »

Hi Hans,

Yes, I'm testing on a very small portion. The end will be most likely be well over 100 pages. This macro works partially. It cleans up those orphan sentences, but when it comes to some paragraphs starting with i, ii, iii, iv, etc, it wraps those in one long paragraph.

Maybe in the end, I just don't know the right process here. I want to take this PDF, copy it to Word and retain the paragraph style. Is there a better way to do this?
Mark 1_1-31 - Detailed Notes.pdf
You do not have the required permissions to view the files attached to this post.

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

Re: Fix broken sentences

Post by HansV »

Try to get hold of the original document instead of the PDF.
If that's not possible, I'd stick with what you have. Trying to fix it will be hell... :evilgrin:
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Fix broken sentences

Post by gailb »

Got it. There isn't any way to get the original PDF. Thanks Hans as always.

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

Re: Fix broken sentences

Post by ChrisGreaves »

gailb wrote:
31 Jan 2021, 15:14
... I have a document with broken sentences.
Hello Gailb. I have over the years developed two applications that clean document text, including resurrecting sentences (and paragraphs) from broken text.
The first is DOCumentCLEanser which was originally used to batch-process thousands of WordPerfect 5.1(DOS) files and neaten them up, but has found use in cleaning text files printed from PDF files. DocCle is rules-driven (so you basically program in its limited but powerful language).
The Second is UNDER-the-hood, which was inspired by Woody Leonhard 's book "The Underground Guide to Word for Windows". This program contains 587 macros in 24 modules. I have attached a TXT list of 55 macros in one of these modules "modTextChange" for your edification.
If you would like to inspect or install the code please let me know.
Cheers
Chris
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Fix broken sentences

Post by gailb »

Hi Chris and thank you for your time. I would absolutely love to install the code. Anything that will help me get further along down the road.

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

Re: Fix broken sentences

Post by ChrisGreaves »

gailb wrote:
02 Feb 2021, 02:28
Hi Chris and thank you for your time. I would absolutely love to install the code. Anything that will help me get further along down the road.
Hi Gail.
(1) Is the attached Word2003 document a fair representation that I can use as a testbed?
(2) Later today I will put together a package that you can download and play with.
The package will, I think, have a working example of DoCle and results of using Under.
Since the package will have the code, you can, if you prefer, cannibalise the VBA code.
Cheers
Chris
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Fix broken sentences

Post by gailb »

Yes, that is an accurate example.

User avatar
Charles Kenyon
4StarLounger
Posts: 596
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Fix broken sentences

Post by Charles Kenyon »

Suzanne Barnhill has a page on this. http://wordfaqs.ssbarnhill.com/CleanWebText.htm
Greg Maxey has an Add-In (free) that you might want to use. https://gregmaxey.com/word_tip_pages/cleanup_text.html

Converted text is often a pain to edit.
I will usually use paste special and paste as plain text. Then apply the formatting using styles in Word.
But, that's just how I am. :grin:

User avatar
Charles Kenyon
4StarLounger
Posts: 596
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Fix broken sentences

Post by Charles Kenyon »

ChrisGreaves wrote:
01 Feb 2021, 12:09
***
The Second is UNDER-the-hood, which was inspired by Woody Leonhard 's book "The Underground Guide to Word for Windows". This program contains 587 macros in 24 modules. I have attached a TXT list of 55 macros in one of these modules "modTextChange" for your edification.
If you would like to inspect or install the code please let me know.
Cheers
Chris
Hi Chris,
I got my start with vba with Woody's "Word 97 Annoyances." That was probably an updated version.
I am a great fan.
Last edited by Charles Kenyon on 02 Feb 2021, 20:52, edited 1 time in total.

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

Re: Fix broken sentences

Post by ChrisGreaves »

gailb wrote:
02 Feb 2021, 12:55
Yes, that is an accurate example.
Hi Gail.
You can download a 1 MB file from chrisgreaves.com/Downloads/20210202_1645.zip.
The package includes a document ReadMe.doc that you can read before making a decision whether this approach might work for you.
I realised last night that my sample 5-page "Mark 1_1-31 - Detailed Notes.doc" is NOT an accurate representation of your PDF file, because I used select All and then copied and pasted the data as text.
That said, regardless of how you reduce that PDF to text, I believe that DoCleanser can do the job, once we have worked out the rules.
I have provided a small RulesTable_working.doc and used two of the rules to effect changes to the text document so that you can see that changes can be automated.
Cheers
Chris
An expensive day out: Wallet and Grimace

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Fix broken sentences

Post by gailb »

Thanks Chris and Charles for your time. I will look into all of these tips and ideas.
Last edited by gailb on 03 Feb 2021, 13:02, edited 1 time in total.

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

Re: Fix broken sentences

Post by ChrisGreaves »

Charles Kenyon wrote:
02 Feb 2021, 20:17
I got my start with vba with Woody's "Word 97 Annoyances." That was probably an updated version. I am a great fan.
Mister Woody Leonhard has a lot to answer for :stop:
He led me into a life of crime. :jollyroger:

I not only stole a lot of his Word6 Macro6 code, but have habitually used one of his opening lines "Let's make this book pay for itself right off the bat" (I paraphrase) as an approach to the design of training courses. :thankyou:
Cheers
Chris
An expensive day out: Wallet and Grimace

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Fix broken sentences

Post by gailb »

Hi Hans,

In post #2, how can that be changed to only apply to the selected text?

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

Re: Fix broken sentences

Post by HansV »

Simples: change ActiveDocument to Selection:

Code: Select all

Sub FixSentencesInSelection()
    Dim i As Long
    Dim rng As Range
    Application.ScreenUpdating = False
    For i = Selection.Paragraphs.Count To 2 Step -1
        Set rng = Selection.Paragraphs(i).Range
        If Not rng.Text Like "?. *" Then
            rng.Collapse
            rng.MoveStart Unit:=wdCharacter, Count:=-1
            rng.Text = " "
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: Fix broken sentences

Post by gailb »

Perfect, thank you.