Log in

View Full Version : Solved: find and replace



white_flag
06-01-2011, 05:45 AM
Hello I have this code and it is not working:


Sub search_insert()

Dim objWdApp As Word.Application
Dim objWdDoc As Word.Document
Dim objWdRange As Word.Range
Dim bFound As Boolean, i As Long
Set objWdDoc = objWdApp.ActiveDocument


objWdApp.Visible = True
start counter at -1
i = -1
Do
Set objWdRange = objWdDoc.Content
With objWdRange.Find
.ClearFormatting
.Replacement.ClearFormatting
'Check if word is found
bFound = .Execute(findtext:="5656565", replaceWith:="found", Replace:=wdReplaceOne)
'increment counter
i = i + 1
End With
Loop While bFound
MsgBox CStr(i) & " replacements made."
Set objWdRange = Nothing
Set objWdDoc = Nothing
Set objWdApp = Nothing
End Sub

I have error '91' on this

Set objWdDoc = objWdApp.ActiveDocument

white_flag
06-01-2011, 06:01 AM
ok I change it but now I have the following:


With ActiveDocument.Content.Find
.Execute FindText:=Me.Controls("Label25").Text, ReplaceWith:="Text for replacement", Format:=True, Replace:=wdReplaceAll
End With



how can I trick VBA to take this
.Execute FindText:=Me.Controls("Label25").Text

white_flag
06-01-2011, 06:27 AM
my bad
.Content

.Execute FindText:=Me.Controls("Label25").Content