我正在使用此函数来替换字符文档中访问的某些字符串。此功能工作得很好VBA中的“with”子句令人讨厌的问题
Sub reemplazar(doc As Word.Document, after As String, before As String, replaceall As Boolean)
With doc.Content.Find
.Text = after
.Replacement.Text = before
.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
但是...我不知道为什么,如果我重写这样它停止工作的功能。没有错误或警告,但没有更换。
Sub reemplazar(doc As Word.Document, after As String, before As String, replaceall As Boolean)
doc.Content.Find.Text = after
doc.Content.Find.Replacement.Text = before
With doc.Content.Find
.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
有人可以解释这两个片段之间的区别是什么或为什么第二个不工作propertyly? 谢谢!
正确的答案 - 原因和答案代码。您可以并且应该清楚地说明您正在枚举第二个代码段的问题。 – jpinto3912 2010-11-22 22:01:51