Word文档中的VBA代码搜索和格式文本 - 无限循环

时间:2015-07-17 02:19:46

标签: vba loops search

我编写了以下代码来搜索单词文档以选择单词之间的文本(即从"隐藏注释"到"隐藏注释")。一旦选择了文本,它就会被着色。我希望代码遍历整个文档一次,但我似乎创建了一个无限循环。任何建议都会非常受欢迎!

谢谢

`Sub SearchShade()

'Start of doc
Selection.HomeKey Unit:=wdStory

'start of loop
Do Until ActiveDocument.Bookmarks("\Sel") = _
ActiveDocument.Bookmarks("\EndOfDoc")

'Search
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Hide Note"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With

'Select down to next Hide Note
Selection.Extend
With Selection.Find
.Text = "Hide Note"
.Forward = True
.Execute
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = -603923969
End With

'Move down one para
Selection.MoveDown Unit:=wdParagraph, Count:=1

'Loop to end of document
Loop

End Sub

1 个答案:

答案 0 :(得分:0)

Do until Selection.Bookmarks.Exists("\EndOfDoc")