Find and replace for Word in Excel Maco not working

hlg0035

New Member
Joined
Sep 8, 2014
Messages
13
Hi all-

I am pretty new to visual basic to please excuse my lack of knowledge on correct nomenclature :)

I have created a code in Excel VBA to open up a template word document and find/replace information in Word with the variables I have created in Excel. This code worked for one program and when I copied it over to create another one it just goes through the steps without finding/replacing anything. No error messages pop up, it just does not work. Here is my code below:
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Users\" & UserID & "\Desktop\Quantien Evaluation Template 9.5.docx")

'FIND AND REPLACE TEXT
With wrdApp.Selection.Find
.Text = "[CustomerName]"
.Replacement.Text = CustomerName
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

End With
wrdApp.Selection.Find.Execute Replace:=wdReplaceAll
 
Welcome to MrExcel.

You can't use Word constants like wdFindContinue with late binding. You need to use the value of the constant.
 
Upvote 0
Thank you Andrew! What suggestions do you have in order to do what you're saying? Once again, excuse my ignorance :) lots to learn over here!

Welcome to MrExcel.

You can't use Word constants like wdFindContinue with late binding. You need to use the value of the constant.
 
Upvote 0
Or add this at the top of the code.
Code:
Const wdFindContinue =1
Const wdReplaceAll = 2

' add declarations for any other Word constants you are using
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top