Find & Replace using Macro in Word

rajtilak

New Member
Joined
May 24, 2016
Messages
4
Code:
Sub FindRiskWords()


Dim oRng As Word.Range
Dim arrExcelValues()
Dim arrExcelValues1()
Dim i As Long
Dim x As Long


Set objExcel = CreateObject("Excel.Application")
Set objExcel1 = CreateObject("Excel.Application")


Set objWorkbook = objExcel.Workbooks.Open("\\path")
objExcel.Visible = False


i = 1
x = 0


Set objWorkbook1 = objExcel1.Workbooks.Open("\\path")
objExcel1.Visible = False


j = 1
y = 0


Do Until objExcel.Cells(i, 1).Value = ""
   ReDim Preserve arrExcelValues(x)
   arrExcelValues(x) = objExcel.Cells(i, 1).Value
   i = i + 1
   x = x + 1
Loop
objExcel.Quit


Do Until objExcel1.Cells(j, 2).Value = ""
    ReDim Preserve arrExcelValues1(y)
    arrExcelValues1(y) = objExcel1.Cells(j, 2).Value
    j = j + 1
    y = y + 1
Loop
objExcel1.Quit


Word.Application.Activate


For i = 0 To UBound(arrExcelValues)
    Set oRng = ActiveDocument.Range
    'Options.DefaultHighlightColorIndex = wdYellow
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = arrExcelValues(i)
        .Replacement.Text = arrExcelValues1(i)
        .MatchWholeWord = True
        .Replacement.Highlight = True
        .Execute Replace:=wdReplaceAll
    End With
Next


End Sub

This code is supposed to find words from an Excel, match is with the corresponding word in another column from that Excel, find those words in a Word file and then replace those words. It's working perfectly except for a small hiccup. When the word found begins with a capital letter, the replaced word is in all caps even if its given in a sentence case in the Excel.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,216,028
Messages
6,128,400
Members
449,448
Latest member
Andrew Slatter

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