Link to home
Start Free TrialLog in
Avatar of KapTheHat
KapTheHat

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KapTheHat
KapTheHat

ASKER

Thanks - why do  I need the 2 after the "execute" ?

Kaps
It's the literal value to Replace All (I guessed that was what you wanted?)
Thanks. much appreciated. will test out later today and get back to you.