Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 

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 11:36 PM.


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