我正在使用此函數來替換字符文檔中訪問的某些字符串。此功能工作得很好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