Ger Cas
Ger Cas

Reputation: 2298

How to remove paragraph marks with different format in MS-Word

I have some lines that are separated by paragraph marks, but actually they belong to the same paragraph.

The issue is the text is in Arial 15, italic, but some words are not italic (shown in red) and the paragraph marks are Arial 15 italic, not italic and even Calibri 11. This means, not all paragraph mark have the same format.

I want to replace the paragraph marks with a space " " in order to join the lines in a single paragraph only for those group of continues lines that are Arial, 15 italic even though thet have not italic within them.

I tried so far:

Find what: ^p (with format: Arial 15, Italic) Replace with: " "

But doing this only finds the paragraph mark shown in green.

Below the image of input format and what I'm trying to get as output. Additionally I attach a sample file Sample.docx.

Sample Image Thanks for any help

Upvotes: 2

Views: 744

Answers (1)

Ahmed AU
Ahmed AU

Reputation: 2777

Try

Option Explicit
Sub ReplacePara()
Dim Para As Paragraph, Xstr As String, Rng As Range
Dim i As Long, ln As Long
Dim PrvChrSize As Integer, NextChrSize  As Integer
Dim PrvChrFont As String, NextChrFont  As String
Dim PrvChrItalic As Boolean, NextChrItalic As Boolean

    With ActiveDocument
    For i = .Paragraphs.Count To 1 Step -1
    Set Para = .Paragraphs(i)
    ln = Para.Range.Characters.Count

        If ln > 1 Then
            With Para.Range.Characters(ln - 1).Font
            PrvChrSize = .Size
            PrvChrFont = .Name
            PrvChrItalic = .Italic
            End With

            If i < .Paragraphs.Count Then
                With .Paragraphs(i + 1).Range.Characters(1).Font
                NextChrSize = .Size
                NextChrFont = .Name
                NextChrItalic = .Italic
                End With
            Else
            NextChrSize = 0
            NextChrFont = ""
            NextChrItalic = False
            End If
        End If

        Debug.Print i, PrvChrSize, PrvChrFont, NextChrSize, NextChrFont
        If (PrvChrSize = 15 And (PrvChrFont = "Arial" Or PrvChrItalic = True)) _
        And (NextChrSize = 15 And (NextChrFont = "Arial" Or NextChrItalic)) Then
        Para.Range.Characters(ln).Text = " "
        End If
    Next
  End With

End Sub

Result from sample file:

enter image description here

Upvotes: 2

Related Questions