请教word中的VBA替换循环的问题
我的目的是在文档中查找所有abc,找到第一个替换成1.abc 找到第二个替换成2.abc .....一直到最后我利用word宏录制了一个VB,又稍微修改一下,但是出现2个问题
第一个是我不知道如何设置循环
用过一次
i=1
Do While Selection.Find.Execute = True
结尾加个i = i + i
loop
但是结果确是 1.abc abc 3.abc abc 5.abc........
第二个用我改的那个不成功的循环,却提示2次已达到结尾,是否从开始搜索,要点2次否
求高手帮忙修改下
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Dim i As Long
For i = 1 To 10
With Selection.Find
.Text = "abc"
.Replacement.Text = i & ".abc"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Next
End Sub
--------------------编程问答-------------------- --------------------编程问答-------------------- 以下代码测试通过:
Sub GetNo()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Dim i As Long
For i = 1 To 10
With Selection.Find
.Text = "abc"
.Replacement.Text = i & ".abc"
.Forward = True
End With
With Selection
If .Find.Forward Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Next
End Sub
补充:VB , VBA