我的宏应该做到以下几点:字VBA宏只能在调试模式
- 查找双重空间的情境。
- 用单个空格替换双空格的实例。
- 再次浏览文档以查看是否还有更多的双空格并替换它们,如果有的话。例如,如果某个地方最初有4个空格,则仍然有双空格,因此,用剩余空格替换剩余的双空格。
- 重复上一步直到没有更多的双空格。
问题是,宏在调试模式下完美工作,但只运行正常运行一次。我究竟做错了什么?请注意,我的代码可能不是最紧凑的,但这不重要;我真正想知道的是为什么代码只能在调试模式下工作,而不是在正常运行模式下工作,以及如何解决这个问题。
Sub Test_for_doubles()
'
' Test_for_doubles Macro
'
Dim blnFoundDoubles As Boolean
blnFoundDoubles = True
Do While blnFoundDoubles = True
Selection.HomeKey Unit:=wdStory 'Go to the beginning of the document.
blnFoundDoubles = False 'Don't go through this loop again unless we find a double this time through
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
If .Found = True Then
blnFoundDoubles = True
End If
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop
End Sub
@CindyMeister:您的建议工作。谢谢! – user6267463