1

I have the code below without using Selection.

Sub Format paragraph()

Dim wdDoc As Document

    With wdDoc.Range.Find 
       .Font.Size = 12
       .Text = "?" 
       .Execute 
    End With
End Sub

When the character with font size = 12 is found, how can I change the format of the current paragraph? for example:

wdDoc.Paragraph(current).Font.Size = 14

wdDoc.Paragraph(current).Font.Color = wdBlue

Thanks for any help.

Ger Cas
  • 2,188
  • 2
  • 18
  • 45

1 Answers1

2

The trick is to work with a specific Range object, which can be used to access its "parent" paragraph. When Find.Execute is successful, the Range being searched contains the found item (same as the selection jumps to the found item). For example:

Sub Format paragraph()
  Dim rng as Range, para as Paragraph
  Dim wdDoc As Document

  Set wdDoc = ActiveDocument. 'Missing in code in question...
  Set rng = wdDoc.Content 'Content returns the Range
    With rng.Find 
       .Font.Size = 12
       .Text = "?" 
       If .Execute = True Then
         Set para = rng.Paragraphs(1)
         para.Font.Size = 14
         para.Font.Color = wdBlue
       End If
    End With
End Sub
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • @Cindy_Meister Hi Cindy. Thanks for your answer. That seems to be what I need. There are several paragraphs, some with font size 12 and others with other fonts. So, how to include your `With rng.find` block in a `while` loop? Or should I count first the paragraphs that match the style I want and then do a `for loop` to apply the same changes to the other paragraphs? – Ger Cas Jun 16 '19 at 21:33
  • @GerCas a loop is the usual way. There are lots of examples for using that with `Find` here and elsewhere. For example https://stackoverflow.com/a/52596708/3077495 Rather than applying manual formatting you might consider applying a *paragraph STYLE* that defines the required formatting. That's just one step and, if the formatting needs to change again at a later point, simply changing the style definition is one-step in the UI - no macro required. – Cindy Meister Jun 17 '19 at 04:55
  • @Cindy_Meister Hi Cindy. Thanks fot you suggestions and link shared. – Ger Cas Jun 17 '19 at 17:31