Excel to Word - Highlight values replaced in a Word document

eikogs

New Member
Joined
Aug 12, 2016
Messages
23
I am using the following code to find and replace values in a Word document with values from my Excel workbook. I would like to highlight values as they are replaced in the word document so the user can identify the values that were added from the Excel sheet. Can someone please help me figure out how to add the highlights as the values are updated in the Word document?


Code:
Sub FindAndReplace(wrdDoc As Object, SearchValue As Variant, NewValue As Variant)[INDENT]Const wdReplaceAll As Long = 2[/INDENT]

                With wrdDoc
                    Set myRange = .Content
                             With myRange.Find
                                    .Text = SearchValue
                                    .MatchWholeWord = False
                                    strReplacement = NewValue
                                    
                                    If Len(strReplacement) > 255 Then
                                        strFragment = Mid(strReplacement, cnt + 1, 230)
                                        strFragment = strFragment & "@@@@@@@@@@"
                                        cnt = cnt + 230
                                        .Replacement.Text = strFragment
                                        .Execute , , , , , , , , , , wdReplaceAll
                                        .Text = "@@@@@@@@@@"
                                        Do
                                           strFragment = Mid(strReplacement, cnt + 1, 230)
                                           cnt = cnt + 230
                                           If Len(strFragment) > 0 Then strFragment = strFragment & "@@@@@@@@@@"
                                           .Replacement.Text = strFragment
                                           .Execute , , , , , , , , , , wdReplaceAll
                                        Loop While Len(strFragment) > 0
                                        cnt = 0
                                    Else
                                         .Replacement.Text = strReplacement
                                         .Execute , , , , , , , , , , wdReplaceAll
                                    End If
                            End With
                End With
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I was able to find a working solution to my problem with the red code below. I would still welcome any suggestions for improvement.

Rich (BB code):
Sub FindAndReplace(wrdDoc As Object, SearchValue As Variant, NewValue As Variant)

                With wrdDoc
                    Set myRange = .Content
                    
                             With myRange.Find
                                    'highlight the search value before replacing it.
                                    Do While .Execute(FindText:=SearchValue)
                                         myRange.HighlightColorIndex = 3 '0=No Highlight, 1=Black, 2=Blue, 3=Turquoise, 4=Green, 5=Pink, 6=Red, 7=Yellow, 8=White, 9=DarkBlue, 10=Teal
                                    Loop
                             
                                    .Text = SearchValue
                                    .MatchWholeWord = False
                                    strReplacement = NewValue
                                    
                                    If Len(strReplacement) > 255 Then
                                        strFragment = Mid(strReplacement, cnt + 1, 230)
                                        strFragment = strFragment & "@@@@@@@@@@"
                                        cnt = cnt + 230
                                        .Replacement.Text = strFragment
                                        .Execute , , , , , , , , , , wdReplaceAll
                                        .Text = "@@@@@@@@@@"
                                        Do
                                           strFragment = Mid(strReplacement, cnt + 1, 230)
                                           cnt = cnt + 230
                                           If Len(strFragment) > 0 Then strFragment = strFragment & "@@@@@@@@@@"
                                           .Replacement.Text = strFragment
                                           .Execute , , , , , , , , , , wdReplaceAll
                                        Loop While Len(strFragment) > 0
                                        cnt = 0
                                    Else
                                         .Replacement.Text = strReplacement
                                         .Execute , , , , , , , , , , wdReplaceAll
                                    End If
                            End With
                End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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