I’m using this function to replace some strings from access in a word document. This function works pretty well
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
But… I don’t know why if I rewrite the function in this way it stop working. There is no error or warning, but the replacement are not made.
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
Can someone explain what is the difference between this two snippets or why the second one is not working propertly?
Thanks!
The Find property returns a Find object every time you call it. So in your second code snippet you’re
That last executed Find object doesn’t have it’s Text or Replacement.Text properties set. If you wanted to use it in this manner, you could create an object variable like