![]() |
|
|
|
#1
|
|||
|
|||
|
It seems like this ought to be really easy. I’m trying to write a macro for MS Word 2010 that will find all words ending in –ly in a document and highlight them. The code below will find the first such word and highlight it, but stops there. The commented-out Do loop doesn’t end if it’s run. Code:
Sub Highlight_adverb()
'
' Highlight_adverb Macro
' Finds and highlights words that end in ly.
'
' Go to the beginning of the piece being checked
'
Selection.HomeKey Unit:=wdStory
'
' Clear any previous search targets
'
Selection.Find.ClearFormatting
'
' Create Do loop that runs until the end of the document is reached
'
' Do
'
' Find example of word ending in "ly ", select the whole word, and highlight it
'
With Selection.Find
.Text = "ly"
.Forward = True
.Wrap = wdFindStop
.MatchSuffix = True
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdTurquoise
Selection.Range.HighlightColorIndex = wdTurquoise
'
' Move forward one word to avoid reselecting the word just highlighted
'
Selection.MoveRight Unit:=wdWord, Count:=1
' Selection.Find.Execute Replace:=wdReplaceAll
'
' ...and loop
'
' Loop While Selection.EndOf(Unit:=wdStory)
End Sub
Code:
Sub highlight_ly_2()
'
' highlight_ly_2 Macro
'
' Go to the beginning of the piece being checked
'
Selection.HomeKey Unit:=wdStory
'
' Find and highlight all ly suffixes
'
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Text = "ly"
.Replacement.Text = "ly"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchSuffix = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Last edited by macropod; 10-15-2012 at 05:34 PM. Reason: Added code tags & formatting |
|
#2
|
||||
|
||||
|
You really don't need a macro for this. A wildcard Find/Replace will do the job, where:
Find = <[! ]@ly> Replace = ^& and the replacement format is set to your highlight colour. If you really want it as a macro, simply switch on the macro recorder and record a wildcard Find/Replace with the above parameters. PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
For a moment, there, Paul, you had me really excited. I'd never seen the wildcard find and replace function before, so I thought I was learning something new. Then I tried it. Didn't work, not even after making sure I included the space after the exclamation point. DRAT! Suggestions?
And yes, I do want this as a macro because it's only step 1 of 3 I eventually want the macro to perform. |
|
#4
|
||||
|
||||
|
Did you check the 'use wildcards' option? The Find expression I posted will find every word ending in 'ly', including words like 'fly', 'ply' & 'sly'. If you wan't to exclude such 3-letter words, you could use:
Find = <[! ][! ]@ly> That'll still find words like 'ally' & 'only', though. You can't really tell Word to find adverbs only.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
||||
|
||||
|
Cross-posted at: http://www.tek-tips.com/viewthread.cfm?qid=1695862
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#6
|
|||
|
|||
|
D'oh!
That was too obvious. Hang on a second while I try again...How cool is THAT??? Regarding your comment about highlighting non-adverbs, you're right--that's step 2. I have a list of the most common nouns and verbs that end in -ly, so my plan was to create an array containing all of them (about 50), have the macro search for them, and unhighlight any that it finds. Is there a smarter way to do that? Thanks! |
|
#7
|
||||
|
||||
|
Well, if you only want to find and highlight particular words, you'd simply use Find/Replace to look for those words, not any word ending in 'ly'. See, for example:
https://www.msofficeforums.com/word/...html#post17703
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#8
|
|||
|
|||
|
Actually, with a bit of tweaking the code you posted over at the reference above looks like it should work (may be a few days before I can try it, though). I'd just substitute the words I want with the ones you listed there (dog,cat,pig...). Since the words the macro would be looking for would already be highlighted, though, I would not want to clear the formatting and would change the .Replacement.Highlight state to False, right?
Looks pretty straight-forward. I'll let you know next week if/how it worked. Thanks again. |
|
#9
|
||||
|
||||
|
If you want to approach it that way, you'd add:
.Highlight = True and change: .Replacement.Highlight = True to: .Replacement.Highlight = False No other changes should be required. The ClearFormatting code clears any format conditions left over from previous Find/Replace instructions - it has no effect on the document content.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#10
|
|||
|
|||
|
Hi, Paul. Good news and bad news. The good news is the code you referred me to to unhighlight specific words works like a champ. So I figured I'd use it again, with the adjustments you recommended, to highlight another set of adverbs. That worked, too! Cool!
The bad news is this: the first macro we discussed--the one that uses wildcards in the find-and-replace function--does something odd. When the word found is the first word in a paragraph, the macro highlights not only it but the last word of the previous paragraph! I tried changing the .MatchWholeWords condition to True but that didn't fix the problem. With just this one problem outstanding, I'm thrilled that things are going so well. |
|
#11
|
||||
|
||||
|
Try changing:
.Text = "<[! ][! ]@ly>" to: .Text = "<[!^09-^13 ][!^09-^13 ]@ly>"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#12
|
|||
|
|||
|
Awesome! That did it! I don't know why you repeated the material in square brackets. I used that just once and it worked.
Now I'm curious: what do ^!, ^09, and ^13 mean? (Newbie question.) Final test: put all the pieces together and see what happens...and...SUCCESS!
|
|
#13
|
||||
|
||||
|
The repeat ensures both 3-letter and 4-letter words are excluded. I'm not immediately aware of any 4-letter adverbs.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#14
|
|||
|
|||
|
I can think of a couple four-letter adverbs off the top of my head: ably and only. There aren't many but they do need to be accounted for. Since I did not include the repeated code, the macro found them.
This is great stuff, Paul. Thanks for your help.
|
|
| Tags |
| find, highlight, suffix |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Find and highlight multiple words in MS Word document
|
AtaLoss | Word VBA | 37 | 09-22-2021 12:04 PM |
| How to find out the duplicates and highlight them? | Learner7 | Excel | 6 | 06-08-2017 06:04 AM |
How to make it highlight blocks of text (words) without highlighting extra space @end
|
seortm | Word | 3 | 03-30-2015 08:12 AM |
| Keyboard shortcut for 'not highlight' in 'Find'? | bertietheblue | Word | 0 | 05-02-2012 06:16 AM |
| find - reading highlight - highlight all / highlight doesn't stick when saved | bobk544 | Word | 3 | 04-15-2009 03:31 PM |