Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and highlight all words ending in -ly

Status
Not open for further replies.

RBLampert

Programmer
Oct 15, 2012
46
US
It seems like this ought to be really easy. I’m trying to write a macro for 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.

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.

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.

 
Hi,

I'd do something like this
Code:
Dim wd as object

For each wd in ThisDocument.Words
   With wd
      If right(trim(.text),2) = "ly" then
          'Format .text here
      End if
   End With
Next

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
...and for the ly only
Code:
Sub test2()
    Dim wd As Object, i As Integer, j As Integer
    
    For Each wd In ThisDocument.Words
       With wd
          If Right(Trim(.Text), 2) = "ly" Then
              i = Len(Trim(.Text))
              For j = i - 1 To i
                .Characters(j).Font.Bold = True
              Next
          End If
       End With
    Next
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
This code makes the procedure a bit more flexible...
Code:
Sub test2()
    Dim wd As Object, i As Integer, j As Integer, sSTR As String, iSTR As Integer
'assign your search string here
    sSTR = "dly"
    
    iSTR = Len(sSTR)
    
    For Each wd In ThisDocument.Words
       With wd
          If Right(Trim(.Text), iSTR) = sSTR Then
              i = Len(Trim(.Text))
              For j = i - (iSTR - 1) To i
                .Characters(j).Font.Bold = True
              Next
          End If
       End With
    Next
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

You're making heavy work of it:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim HlLt As Long
HlLt = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdTurquoise
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "<[! ][! ]@ly>"
  .Replacement.Highlight = True
  .Replacement.Text = "^&"
  .Forward = True
  .Wrap = wdFindStop
  .Format = True
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
Options.DefaultHighlightColorIndex = HlLt
Application.ScreenUpdating = True
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
@Paul. No doubt I am. Just using what Word I know, which wets the bottom of a thimble. ;-)

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip, thanks for your suggestions. Paul's got a method that works great for the first step of the 3-part process I ultimately want this macro to perform. Stay tuned--I may well need help with parts 2 and 3. [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top