Мне нужно выделить два элемента из списка - PullRequest
0 голосов
/ 07 октября 2019

Мне нужно выделить текст из пункта № 3 из моего списка, но не предшествующее число, ни точку, ни пробел между периодом и текстом.

В моем макросе есть подпрограмма, которая выделяет абзацы, но в списке она работает неправильно. Я нашел в Интернете некоторый код (опубликован ниже, 2-й подпункт), который выделяет абзац в списке, но выделяет '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

Макрос выделяет число в дополнение к тексту;Я только хочу, чтобы он выделил текст, а не предыдущее число, ни точку, ни пробел между «3». и "Это"

1 Ответ

0 голосов
/ 07 октября 2019

Уловка, которую я использую, состоит в том, чтобы выделить первый символ и остальную часть абзаца отдельно, например, так:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...