仅突出显示列表中的项目的文字

问题描述 投票:0回答:1

我需要突出显示列表中项目#3的文本,但不要突出显示前面的数字,句号,也不要突出显示句号和文本之间的空格。

我有一个突出显示段落的子项,但是在列表中它不能正常工作。我在网上找到了一些代码(在下面的第二个子代码中发布),该代码突出显示了列表中的一个段落,但突出显示了'3。 ”,这是我不想的。

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

宏会突出显示文本以外的数字。我只想突出显示文本。

vba ms-word
1个回答
0
投票

我使用的技巧是分别突出显示该段落的第一个字符和其余部分,如下所示:

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
© www.soinside.com 2019 - 2024. All rights reserved.