Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-15-2012, 09:33 AM
RBLampert RBLampert is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 64bit
Novice
Find and highlight all words ending in -ly
 
Join Date: Oct 2012
Posts: 9
RBLampert is on a distinguished road
Question Find and highlight all words ending in -ly

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
This code (below), on the other hand, finds And highlights all –ly suffixes but Not the complete word.
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
How do I get the code to do both? I feel like I’m missing one simple command but have no idea what it is.

Last edited by macropod; 10-15-2012 at 05:34 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 10-15-2012, 05:31 PM
macropod's Avatar
macropod macropod is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 10-15-2012, 09:49 PM
RBLampert RBLampert is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 64bit
Novice
Find and highlight all words ending in -ly
 
Join Date: Oct 2012
Posts: 9
RBLampert is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 10-15-2012, 10:13 PM
macropod's Avatar
macropod macropod is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 10-16-2012, 04:08 AM
macropod's Avatar
macropod macropod is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #6  
Old 10-16-2012, 08:19 AM
RBLampert RBLampert is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 64bit
Novice
Find and highlight all words ending in -ly
 
Join Date: Oct 2012
Posts: 9
RBLampert is on a distinguished road
Default

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!
Reply With Quote
  #7  
Old 10-16-2012, 02:39 PM
macropod's Avatar
macropod macropod is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 10-16-2012, 03:52 PM
RBLampert RBLampert is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 64bit
Novice
Find and highlight all words ending in -ly
 
Join Date: Oct 2012
Posts: 9
RBLampert is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 10-16-2012, 05:55 PM
macropod's Avatar
macropod macropod is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #10  
Old 10-23-2012, 03:55 PM
RBLampert RBLampert is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 64bit
Novice
Find and highlight all words ending in -ly
 
Join Date: Oct 2012
Posts: 9
RBLampert is on a distinguished road
Default

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.
Reply With Quote
  #11  
Old 10-23-2012, 04:00 PM
macropod's Avatar
macropod macropod is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try changing:
.Text = "<[! ][! ]@ly>"
to:
.Text = "<[!^09-^13 ][!^09-^13 ]@ly>"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 10-23-2012, 04:19 PM
RBLampert RBLampert is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 64bit
Novice
Find and highlight all words ending in -ly
 
Join Date: Oct 2012
Posts: 9
RBLampert is on a distinguished road
Default

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!

Reply With Quote
  #13  
Old 10-23-2012, 04:35 PM
macropod's Avatar
macropod macropod is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #14  
Old 10-23-2012, 04:45 PM
RBLampert RBLampert is offline Find and highlight all words ending in -ly Windows 7 64bit Find and highlight all words ending in -ly Office 2010 64bit
Novice
Find and highlight all words ending in -ly
 
Join Date: Oct 2012
Posts: 9
RBLampert is on a distinguished road
Default

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.
Reply With Quote
Reply

Tags
find, highlight, suffix



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and highlight all words ending in -ly 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
Find and highlight all words ending in -ly 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:22 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft