?
Solved

VBA Word - Find / Replace

Posted on 2009-04-27
5
Medium Priority
?
1,272 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:KapTheHat
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1200 total points
ID: 24240202
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
 

Author Comment

by:KapTheHat
ID: 24240386
Thanks - why do  I need the 2 after the "execute" ?

Kaps
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24240430
It's the literal value to Replace All (I guessed that was what you wanted?)
0
 

Author Comment

by:KapTheHat
ID: 24240663
Thanks. much appreciated. will test out later today and get back to you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question