0
votes

Im trying to fix my word document's empty paragraphs with VBA. The issue is that i have pages with paragraphs that have a font.size = 11. I need to change these paragraphs to font.size = 10. So i need a loop that starts from the beginning of the document, iterates through the paragraphs and searches If paragraph is empty AND font.size = 11 then Font.Size 10.

I keep getting an errors and not quite sure if im trying to do the right thing. Any help?

With Selection
Dim Paragraph As Word.Paragraph
For Each Paragraph In ActiveDocument.Paragraphs
If Paragraph.Range.Count = 1 And Font.Size = 11 Then
Paragraph.Font.Size = 10
Next Paragraph
End With
1
The With statement does not do what you think it does.braX
@Carlsberg789 Your entire approach is wrong. You should not have empty paragraphs. You should instead use Word's paragraph 'space before' and 'space after' settings to control the inter-paragraph spacing. And, moreover, this should be achieved via the relevant paragraph Styles, not by direct formatting. Your current approach not only adds clutter to the document but also renders it more liable to corruption.macropod

1 Answers

1
votes

There are several reasons your code doesn't work.

With Selection
   ...
End With

This statement is not used and you have no reason to have it here. With statements always go together with expressions starting with ..

The If .. Then statement needs an End If since you aren't doing it all in one line.

The Font property is not directly connected to the Paragraph. There is a Range object too.

Put it all together:

Dim Paragraph As Word.Paragraph
For Each Paragraph In ActiveDocument.Paragraphs
    If Len(Paragraph.Range.Text) <= 1 And Paragraph.Range.Font.Size = 11 Then
        Paragraph.Range.Font.Size = 10
    End If
Next Paragraph