View Single Post
 
Old 09-26-2022, 07:38 PM
Nicknamednick Nicknamednick is offline Windows 10 Office 2021
Novice
 
Join Date: Sep 2022
Posts: 4
Nicknamednick is on a distinguished road
Default Searching a Word document with multiple words from a list (over 1,000 terms) and BOLD all occurrence

Hi everyone! I use Microsoft 365 for business. I am just learning about macros in Word, and I'm struggling with a problem I'm trying to solve. I have put together a list of 1,129 words and phrases that, if they appear in my weekly Word document, need to be in Bold print. Some of the phrases have commas, and some may have apostrophes. There are definitely quite a few with dashes, and some slashes.


Note: I have the list in an Excel workbook right now.


I am trying to create a macro that will draw the list of terms from the excel workbook, search my document for each term and emBolden them as they are found.



So far, I've tried the following code. It is a slightly revised copy of an answer I found at stackoverflow for a similar question. They wanted highlighting, I believe, but otherwise it was very similar to my problem.

Sub Pre_List_for_Bold() ' Sub Pre_List_for_Bold()
Dim xl As Object 'Excel.Application
Dim wb As Object 'Excel.Workbook
Dim ws As Object 'Excel.Worksheet
Dim rng As Object 'Excel.Range
Dim cl As Object 'Excel.Range
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open("C:\biglist.xlsx") '## Modify as needed
Set ws = wb.Sheets(1) '##Modify as needed
Set rng = ws.Range("B3", ws.Range("B3").End(xlDown))
For Each cl In rng
Call EMBOLD_NEC_LIST(cl.Value, cl.Offset(0, 1).Value)
Next
End Sub

Sub EMBOLD_NEC_LIST(findText$, replaceText$)
'
' EMBOLD_NEC_LIST Macro
'
'
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = findText
.Replacement.Text = replaceText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute
End Sub

'
End Sub


However, when I run the macro, I get the following error message:


Run-time error 1004: Application-defined or object-defined error


When I hit debug, it highlights the line I put in Red text above "Set rng = ws.Range("B3", ws.Range("B3").End(xlDown))"


I can't see what is wrong with the line, but then again I've only started learning this. If anyone has some ideas, I would be very grateful for the help.


Thank you!

Last edited by Nicknamednick; 09-27-2022 at 02:32 AM. Reason: adding error message image