我正在尝试在 Word 中创建一个 VBA 宏来检查段落文本是否由点组成。如果为真,我想添加更多点,直到整行都充满点。我面临的问题是我的宏进入无限循环。 我有以下代码:
Sub Dots()
Application.ScreenUpdating = False
Dim doc As Document
Set doc = ActiveDocument
Dim paragraph As paragraph
For Each paragraph In doc.Paragraphs
If Left(paragraph.Range.Text, 6) = "......" Then
If Right(paragraph.Range.Text, 5) = "(NR)" & vbCr Then
paragraph.Range.Text = String(112, ".") & " (NR)" & vbCr
ElseIf Right(paragraph.Range.Text, 4) = "..." & vbCr Then
paragraph.Range.Text = String(122, ".") & vbCr
End If
End If
Next paragraph
Application.ScreenUpdating = True
End Sub
我想问题是,最后用 vbCr 更改段落文本后,下一个循环再次分析同一段落。但如果我不添加 vbCr,该段落将与下一个段落合并。 请问有什么想法吗?
使用
For
循环并向后循环:
Dim i As Long
For i = doc.Paragraphs.Count To 1 Step -1
Dim paragraph As paragraph
Set paragraph = doc.Paragraphs(i)
' rest of your code
Next i