Find属性在每次调用Find对象时返回该对象。所以在你的第二段代码中
-
创建查找对象并设置其文本属性
-
创建新的Find对象并设置其替换.Text属性
-
创建第三个Find对象并设置一组其他属性并执行
上次执行的Find对象没有设置它的Text或Replacement.Text属性。如果您想以这种方式使用它,可以创建一个对象变量,如
Sub reemplazar(doc As Word.Document, after As String, before As String, replaceall As Boolean)
Dim fnd As Find
Set fnd = doc.Content.Find
fnd.Text = after
fnd.Replacement.Text = before
With fnd
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
If replaceall Then
.Execute Replace:=wdReplaceAll
Else
.Execute Replace:=wdReplaceOne
End If
End With
End Sub