Excel to Word: Search and Replace VBA

kmh6278

Board Regular
Joined
Mar 7, 2006
Messages
52
Hi,

I have a list of words maintained in Excel that will be used for searching and replacing in Word. I have code that originates in Excel, loads the list of words from the Excel file into an array, and then opens a Word file to search and replace for each word in that array. The code seems to work for the most part, however, the "replaceall" isn't working once in Word. Stepping through the code, everything seems to be working as intended until the last line.

Here is the code I am working with. Any help would be greatly appreciated as I am stuck!

Sub SpellCheck()

Dim replace_text As String
Dim verbTemplateWord As Variant
Dim this_index As Variant, this_word As Variant, last_word As Variant
Dim DataList As Range, word_list As Variant

Set DataList = Sheets("Sheet2").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
word_list = DataList.Value ' this is where it loads the values in column A

replace_text = "XXXXXX" 'replace items in word_list with this

'now need to switch to word
' this opens Word (object named wrdApp)
On Error Resume Next
Set wrdApp = GetObject("Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wrdApp = CreateObject("Word.Application")
End If
On Error GoTo 0


' COMMENT OUT (for debugging only)


wrdApp.DisplayAlerts = True
wrdApp.Visible = True ' can set this to true (and ScreenUpdating) for debugging
wrdApp.ScreenUpdating = True

' Open Word template
verbTemplateWord = "f:\home\kristin\reference\vba_test\SpellCheck Test.docx"
Set wrdDoc = wrdApp.documents.Open(verbTemplateWord)

'this block searches, finds but doesn't replace. for some reason, it highlights instead?
last_word = UBound(word_list)
For this_index = 1 To last_word ' the main loop through the word list
this_word = word_list(this_index, 1)

wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Replacement.ClearFormatting

With wrdApp.Selection.Find
.Text = this_word
.Replacement.Text = replace_text
.Forward = True
.Wrap = wdFindcontinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wrdApp.Selection.Find.Execute Replace:=wdReplaceAll

Next this_index


End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,215,956
Messages
6,127,931
Members
449,411
Latest member
AppellatePerson

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