|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
Tags |
excel 2019, ms-word, vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Wildcards: searching for multiple words / expressions close to each other | ballpoint | Word VBA | 7 | 11-09-2017 03:30 PM |
Best Practice for Indexing Multiple Word Terms and Sub-Terms | jhy001 | Word | 4 | 11-06-2017 02:08 PM |
All words in document appear bold but are not! | Ezra | Word | 4 | 07-31-2017 06:53 AM |
Making Multiple Words Bold | mtk989 | Word | 2 | 06-25-2011 11:27 AM |
How to make a list of all the bold words in a document? | galaxy_110 | Word | 1 | 12-31-2010 08:23 AM |