我需要选择文档中的每三行,并将文本格式化为粗体、下划线和蓝色。我已经有这个代码了,但我需要循环一定次数。
问题是,我发现的所有循环教程都使用变量或状态来执行此操作,直到满足某些条件。我如何将该条件设置为“到达文档底部”?
我设置的代码如下,括号中的行是我需要帮助的内容:
Selection.HomeKey Unit:=wdStory
[Do the following code until the end of the document]
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
With Selection.Font
.Bold = wdToggle
.Color = 12611584
.Underline = wdUnderlineSingle
End With
Selection.MoveDown Unit:=wdLine, Count:=3
[end of loop]
非常基本的问题。我熟悉如何在 excel 中设置这些类型的循环,但在 word 中,我无法弄清楚。我感谢任何花时间回答的人。
答案1
这应该有效
Dim lastRow As Long
lastRow = ActiveDocument.BuiltInDocumentProperties("Number Of Lines")
MsgBox lastRow
因此,像这样的代码可以工作,并且与 excel 的循环类型相同for
-
Sub CountLines()
Dim lastRow As Long
Dim i As Long
lastRow = ActiveDocument.BuiltInDocumentProperties("Number Of Lines")
For i = 1 To lastRow
If i Mod 3 = 0 Then
'Do Stuff
End If
Next
End Sub
或者更彻底地说 -
Sub CountLines()
Dim lastRow As Long
Dim i As Long
lastRow = ActiveDocument.BuiltInDocumentProperties("Number Of Lines")
For i = 1 To lastRow
If i Mod 3 = 0 Then
ActiveDocument.Paragraphs(i).Range.Font.Bold = True
'Do other stuff
End If
Next
End Sub