0
votes

My goal is to merge multiple paragraph based on predefined string (in an arrary) such as period (.) or question mark (?)

My sample sentence is as below (refer the image please):

Input

The expected result will be something like this.

////////////////////////////////////////////////////////

Output

////////////////////////////////////////////////////////

In the below code, I could achieve it using period (.), but for every other end string, I have separate macro. Depends on the end string, I run different macro. Is there way to put all these search string (. / ? / ;) in a single array and ask the code to run until it find either of them and exit from the loop and do the merge?

Blockquote

Sub FindDotToJoinParagraph()
Dim xRange As Range
Dim Srt As Variant
Dim Endee As Variant
Dim currentPosition As Range



Selection.HomeKey Unit:=wdLine, Extend:=wdMove  
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Srt" 'Bookmark

With Selection.Find
    .Text = "." 'Here not just period alone, but others too 
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdMove
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Ends" 'Bookmark

ActiveDocument.Range( _
    ActiveDocument.Bookmarks("Srt").Range.Start, _
    ActiveDocument.Bookmarks("Ends").Range.Start) _
    .Select

call MergeParaAndLineBreaks() 'An another sub-routine to merge 

End Sub

////////////////////////////////// Sub MergeParaAndLineBreaks()

Dim oRng As Range
Set oRng = Selection.Range

Dim oFind As Range
Set oFind = Selection.Range

With oFind.Find
    Do While .Execute(findtext:="[^13^l]{1,}", MatchWildcards:=True)
        If oFind.InRange(oRng) Then
            oFind.Text = ""
        End If
    Loop
End With
Set oFind = oRng

With oFind.Find
    Do While .Execute(findtext:="[ ]{2,}", MatchWildcards:=True)
        If oFind.InRange(oRng) Then
            oFind.Text = Chr(32)
            oFind.Collapse 0
        End If
    Loop
End With

lbl_Exit:
Set oRng = Nothing
Set oFind = Nothing
Exit Sub
End Sub

Blockquote

Could someone help me out, please!

1
You need to update your example with a screen shot of the text in Word with the hidden formatting showing. At the moment it is not clear if you paragraphs are paragraph or just section of text within a paragraph.freeflow
Your description suggests you're trying to clean up the formatting of text that's been copied from websites, e-mails, and/or PDFs. In that case, see, for example: msofficeforums.com/word/…macropod
Hi @Freeflow, as per your recommendation, added images for both input and output. Thank you for looking at itmannar mani
@macropod The input is not really from PDF, but sort of Yes. Thank you for the link. I checked it. Unfortunately it does not help.mannar mani
Did you actually try the code there - and make sure your text is appropriately structured beforehand? Saying only "Unfortunately it does not help" isn't conducive to finding a solution. After all, not every sentence terminating a line in a document also terminates a paragraph. Colons & semi-colons, too, may be in-line and your approach could end up with some items following a colon being put in separate paragraphs and other items following the same colons being put in the same paragraphs.macropod

1 Answers

0
votes

As noted in my comments, paragraphs don't necessarily end at punctuations marks. That said, a crude but effective solution to achieve the results you've described would be to use a wildcard Find/Replace, where:

Find = ([!.\!\?;:])^13
Replace = ^32\1

IOW, you don't even need a macro. Since we don't know whether there are spaces preceding the paragraph breaks in your source text, you may end up with some extra spaces - which you could clean up with another Find/Replace (or you might omit the ^32 from the Replace expression).