Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 09-05-2018, 04:56 PM
Nanaia Nanaia is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Dec 2016
Location: Lexington, KY
Posts: 10
Nanaia is on a distinguished road
Default Highlight words from a list

I'm trying to create a macro so that when it's applied it will highlight specific words from a list of words I have. I've got the macro started however I can't seem to figure out how to keep it from highlighting words within words. For example, I want it to highlight the word in, which it will do, but it also highlights it in the word involved and being. Any help, or recommendation for an alternate macro is appreciated. I know I ran across something like this once before but am having difficulty relocating it. This is what I have so far:

Code:
Sub ListChange()
Dim r As Range
Dim MyList() As String
Dim i As Long
MyList = Split("additionally,ah,almost,big,can be,could,could be,generally speaking,he,in,it,it is,low,many,may,might,most,plenty,she,should,some,these,they,this is,we,with", ",")
For i = 0 To UBound(MyList())
   Set r = ActiveDocument.Range
      With r.Find
         .Text = MyList(i)
         .Replacement.Highlight = wdYellow
         .Execute Replace:=wdReplaceAll
         .MatchWildcards = False
         .MatchWholeWord = True
         .MatchAllWordForms = False
      End With
Next
End Sub


Last edited by macropod; 09-05-2018 at 07:11 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 09-05-2018, 07:13 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,468
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Your macro isn't far off being correct. Try:
Code:
Sub ListChange()
Application.ScreenUpdating = False
Dim MyList() As String, i As Long
Options.DefaultHighlightColorIndex = wdYellow
MyList = Split("additionally,ah,almost,big,can be,could,could be,generally speaking,he,in,it,it is,low,many,may,might,most,plenty,she,should,some, these,they,this is,we,with", ",")
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Replacement.Highlight = True
  .Replacement.Text = "^&"
  .MatchWholeWord = True
  For i = 0 To UBound(MyList())
    .Text = MyList(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Application.ScreenUpdating = True
End Sub
Do note that you'll also get word forms ending with 's highlighted, but the 's won't be highlighted.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 09-07-2018, 10:06 AM
Nanaia Nanaia is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Dec 2016
Location: Lexington, KY
Posts: 10
Nanaia is on a distinguished road
Default

Should this macro work differently between a Mac and Windows computer? When I added to my work computer (Windows 7) it did wonderfully. When I added it to Word on my MacBook it continues to highlight the words within the words. Is there something I need to do differently?
Reply With Quote
  #4  
Old 09-07-2018, 02:13 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,468
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by Nanaia View Post
Should this macro work differently between a Mac and Windows computer?
No, it should work on both the same way that it works on the PC. Evidently, there's something wrong with the Mac Office installation. I suspect it's a Word bug, as I'm aware of other discussions where a given Find/Replace operation has not been working correctly on Macs but works just fine on PCs.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find (highlight) two and more words in a list of 75k single words in Word 2010 Usora Word 8 05-29-2018 03:34 AM
Macro to highlight a list of words bakerkr Word VBA 4 10-19-2017 02:23 PM
Highlight Words from a Word List JSC6 Word VBA 1 09-30-2014 08:22 PM
Using macros to highlight worksheet words referenced in external list/doc. Daniel_NYC Word VBA 1 04-21-2014 03:35 PM
VBA to highlight words if used too much aolszewski Word VBA 3 11-23-2013 02:07 AM


All times are GMT -7. The time now is 01:41 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft