简体   繁体   中英

Looping through paragraphs in Word - VBA

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM