Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filesearch - replace string

Status
Not open for further replies.

NNNNN

MIS
Dec 2, 2002
90
GB
Hi

An exapple of what I am trying to do is:
find the string "ABC" in word docs and replace it with the string "123"
Wverything works except for the replace bit

any ideas? thanks

Dim fnd As String
Dim wrd As Object
Dim str As String

With Application.FileSearch
.NewSearch
.LookIn = "C:\"
.SearchSubFolders = True
.FileName = "*.doc"
.TextOrProperty = "ABC"
.MatchTextExactly = True


If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
fnd = fnd & Chr(13) & Chr(10) & .FoundFiles(i)
Set wrd = CreateObject("Word.Application")
wrd.Visible = True
wrd.Documents.Open .FoundFiles(i)

With wrd.Selection.Find
.Text = "ABC"
.Replacement.Text = "123"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wrd.Selection.Find.Execute Replace:=wdReplaceAll

Next i
Set wrd = Nothing
Else
MsgBox "There were no files found."
End If

End With
Txtfound.Value = fnd
 
just a guess - are you saving the document after you do the replace?
 
yes
Sorry I left that bit out of the code, I was saving and then closing but I removed so that it wouldnt close as I wanted to see what was going on
the find seems to work but not the replace?

Thanks
 
What about Option Explicit ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top