0
votes

I need to highlight the text from item #3 from my list, but not the preceding number, nor the period, nor the space between the period and the text.

I have a sub that highlights paragraphs, but it does not work correctly in a list. I found some code online (posted below, the 2nd sub) that highlights a paragraph in the list, but it highlights the '3. ' part, which I don't want.

Sub CreateList()
'This is the code that creates the list in which one line needs to be highlighted
        With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
            .NumberFormat = "%1."
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleArabic
            .NumberPosition = InchesToPoints(0)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(0.25)
            .TabPosition = wdUndefined
            .ResetOnHigher = 0
            .StartAt = 1
            .LinkedStyle = ""
        End With
        ListGalleries(wdNumberGallery).ListTemplates(1).Name = ""
        Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior
        Selection.Typetext text:="This is the first line of text"
        Selection.Typetext text:="This is the second line of text"
        Selection.Typetext text:="This is the line of text that I want highlighted that includes the phrase: hereby make"
        Selection.Typetext text:="This is the fourth line of text that should not be highlighted"

End Sub


Sub HighlightList()
'
' HighlightList Macro. This is the code I found online.
'

    With ActiveDocument.Range
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "hereby make"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchWildcards = True
            .Execute
        End With
        Do While .Find.Found
            .Duplicate.Paragraphs.First.Range.HighlightColorIndex = wdYellow
            .Start = .Duplicate.Paragraphs.First.Range.End
            .Find.Execute
        Loop
    End With

End Sub

The macro highlights the number in addition to the text. I only want to highlight the text.

1

1 Answers

0
votes

The trick I use is to highlight the first character and the rest of the paragraph separately, like so:

Sub HighlightList()

    ActiveDocument.Range(0, 0).Select
    With Selection
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "hereby make"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchWildcards = True
            .Execute
        End With
        Do While .Find.Found
            .Paragraphs.First.Range.Select
            .Collapse wdCollapseStart
            .MoveEnd wdCharacter, 1
            .Range.HighlightColorIndex = wdYellow
            .Paragraphs.First.Range.Select
            .MoveEnd wdCharacter, -1
            .MoveStart wdCharacter, 1
            .Range.HighlightColorIndex = wdYellow
            .Collapse wdCollapseEnd
            .Find.Execute
        Loop
    End With

End Sub