VBA Word - Find / Replace

I am trying to use VBA to do Find / Replace in Word. I have used the macro recorder as a starting point but it doesn't work.  I have given a section of the code below. It does n't like the find / text approach. Thanks

Kaps
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim Doc_to_Open As string
 
Dim old_First_Name as String
Dim new_First_Name as String
 
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
 
 
Doc_To_Open = Sheets("Front Sheet").ComboBox2.Value
 
 
Set wrdDoc = wrdApp.Documents.Open(Full_Doc_Name)
 
 
 
With  wrdDoc.find
.text =Old_First_Name
.replacement.text  = New_First_name
end with
 
Old_first_name = new_first_name

Open in new window

KapTheHatAsked:
Who is Participating?
 
Rory ArchibaldConnect With a Mentor Commented:
Two things - firstly the Find is not a property of the Document; secondly, you neveer actually execute the Find! Try:
 

With  wrdApp.Selection.find
.text =Old_First_Name
.replacement.text  = New_First_name
.execute 2
end with
 
Old_first_name = new_first_name

Open in new window

0
 
KapTheHatAuthor Commented:
Thanks - why do  I need the 2 after the "execute" ?

Kaps
0
 
Rory ArchibaldCommented:
It's the literal value to Replace All (I guessed that was what you wanted?)
0
 
KapTheHatAuthor Commented:
Thanks. much appreciated. will test out later today and get back to you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.