简体   繁体   English

在 Word 中循环段落 - VBA

[英]Looping through paragraphs in Word - VBA

Im trying to fix my word document's empty paragraphs with VBA.我试图用 VBA 修复我的 word 文档的空段落。 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.问题是我的页面中的段落的 font.size = 11。我需要将这些段落更改为 font.size = 10。所以我需要一个从文档开头开始的循环,遍历段落并搜索如果段落为空且 font.size = 11 则 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 . With语句总是与以 开头的表达式一起使用. . .

The If .. Then statement needs an End If since you aren't doing it all in one line. If .. Then语句需要一个End If因为您不是在一行中完成所有操作。

The Font property is not directly connected to the Paragraph . Font属性不直接连接到Paragraph There is a Range object too.还有一个Range对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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