1
votes

I have a MS Word document (2016) containing text with different fonts like this

Abc.q

Defq

// Abc. has font size = 20 pt and q that represents paragraph mark has font size=10 pt

// Def has font size = 16 pt and q (paragraph mark) has font size=10 pt

The paragraph mark is equivalent to carriage return CR (^13)

Then, how can I replace the paragraph mark with string but only for the texts that have font size = 20 pt, this is in this case only Abc. and result would be Abc.string using Advance Find/Replace or VBA?

I tried:

Find: ^13 --> with font 10pt
Replace: string

But that replaces paragraph mark with string in all cases. I even tried creating a new style with font = 20.pt and in option Style for following paragraph I set q Normal (Where q represents paragraph mark).

Thanks for any help.

1

1 Answers

1
votes

It could simply done (without using Find method) like this

Sub ReplacePara()
Dim Para As Paragraph, Xstr As String, Rng As Range
Xstr = "String to be added"
For Each Para In ActiveDocument.Paragraphs
ln = Para.Range.Characters.Count
    If ln > 1 Then
    If Para.Range.Characters(ln - 1).Font.Size = 20 Then
    Para.Range.Text = Left(Para.Range.Text, ln - 1) & Xstr
    Set Rng = ActiveDocument.Range(Start:=Para.Range.Start, End:=Para.Range.Start + ln - 1 + Len(Xstr))
    Rng.Font.Size = 20
    End If
    End If
Next
End Sub

Tested to achieve what I understand as your requirement Edited to assign a Font size of preceding text (i.e 20) for added string. Font name, Bold, italics etc properties (gathered by Para.Range.Characters(ln - 1).Font........ to a variable before replacement) could also be assigned to added text in same way.