【问题标题】:Highlight current word when press right or left arrow按向右或向左箭头时突出显示当前单词
【发布时间】:2018-06-22 15:28:53
【问题描述】:

我想开发一个程序,当按下右箭头或左箭头时,当前单词会突出显示。因此,当我使用箭头键向左或向右移动时,光标所在的单词会突出显示。其中一部分我能够利用以下信息进行开发 highlight current line of word document when move up or down

对于我目前拥有的右箭头:

Sub SelectWordRight()
Application.ScreenUpdating = False

ActiveDocument.Content.HighlightColorIndex = wdNoHighlight

Selection.MoveStart wdWord, 1
Application.Selection.Words(1).Select

Selection.Range.HighlightColorIndex = wdYellow

'Unselect the line
Application.Selection.Collapse wdCollapseStart

Application.ScreenUpdating = True
End Sub

左箭头

Sub SelectWordLeft()
Application.ScreenUpdating = False
ActiveDocument.Content.HighlightColorIndex = wdNoHighlight

Selection.MoveEnd wdWord, -1

Application.Selection.Words(1).Select
Selection.Range.HighlightColorIndex = wdYellow

'Unselect the line
Application.Selection.StartOf
Application.ScreenUpdating = True
End Sub

效果很好。但是当有像左括号和右括号这样的边界字符的单词时,它只会突出显示括号。

例如考虑这个 - 我喜欢这个网站(stackoverflow)

当我按下其中一个箭头时,它适用于其他词。但是当涉及到 (stackoverflow) 时,它会分别突出显示 (, stackoverflow 和 )。那么有没有办法突出显示以空格分隔的单词?

在 Cindy 的回答的帮助下,我能够为这样的左箭头开发解决方案。

Sub SelectWordLeft()
Application.ScreenUpdating = False
ActiveDocument.Content.HighlightColorIndex = wdNoHighlight

Selection.MoveEnd wdWord, -1

Selection.MoveStartUntil " ", wdBackward
Selection.MoveEndUntil " ", wdForward
Selection.Range.HighlightColorIndex = wdYellow

'Unselect the line
Application.Selection.StartOf
Application.ScreenUpdating = True
End Sub

但我仍然无法弄清楚如何将其转换为在右箭头中使用。当有 - : ) ( { }

之类的字符时,它会失败

【问题讨论】:

  • 这是一个有趣的问题。请注意,您修改后的 SelectWordLeft 不适用于“Stackoverflow.com 是一个很棒的网站”之类的内容。 - 即当文本以“复杂”单词开头并且没有前面的空格时 - 但可能是一个小细节。我只是想看看最佳解决方案。

标签: vba ms-word


【解决方案1】:

这适用于右箭头,请检查一下。

Sub SelectWordRight()
    Application.ScreenUpdating = False

    ActiveDocument.Content.HighlightColorIndex = wdNoHighlight

MoveToStartOfANewWord:      ' I added this label also

    Selection.MoveStart wdWord, 1

    ' I Added this BLOCK over here
    '-----------------------------
    ' PURPOSE: Move to the start of a -
    ' word i.e. our definition of a word

    'Debug.Print "'" & Selection.Previous & "'" & _
                " - " & Asc(Selection.Previous)
    If (Asc(Selection.Previous) = 32) Or _
        (Asc(Selection.Previous) = 13) _
    Then
        'check 4 "space" then "paragraph"
        GoTo ItIsAtTheStartOfANewWord
    Else
        GoTo MoveToStartOfANewWord
    End If

ItIsAtTheStartOfANewWord:


    Application.Selection.Words(1).Select

    ' I Added this BLOCK over here
    '-----------------------------
    ' PURPOSE: To extend the selection
    ' till the begining of a new word

ExtendTillStartOfANewWord:

    'Debug.Print "'" & Selection.Characters.Last & "'" & _
                " - " & Asc(Selection.Characters.Last)
    If (Asc(Selection.Characters.Last) = 32) Or _
       (Asc(Selection.Characters.Last) = 13) _
    Then
        'Selection.MoveEndUntil Cset:=" "
        GoTo SelectionIsAtTheStartOfANewWord
    Else
        Selection.MoveEnd wdWord, 1
        GoTo ExtendTillStartOfANewWord
    End If

SelectionIsAtTheStartOfANewWord:


    Selection.Range.HighlightColorIndex = wdYellow

    'Unselect the line
    Application.Selection.Collapse wdCollapseStart

    Application.ScreenUpdating = True

End Sub

它看起来很多,但效果很好,尤其是在段落中断。我希望你可以修改它为左箭头。

【讨论】:

  • 天啊。它工作得很好。非常感谢您为解决这个问题付出了这么多努力。
  • 很高兴您发现它很有用。如果您在使其适用于左箭头时遇到任何挑战,我很乐意提供帮助。
【解决方案2】:

您可以使用一个宏来执行此操作 - 可以将一个宏分配给多个键。

建议的方法使用MoveStartUntilMoveEndUntil 方法来扩展范围(或选择),直到遇到指定的字符集(在本例中为空格)。

Sub SelectWord()
    Dim rng As Word.Range    
    Application.ScreenUpdating = False

    ActiveDocument.content.HighlightColorIndex = wdNoHighlight
    Set rng = Selection.Range 
    rng.MoveStartUntil " ", wdBackward
    rng.MoveEndUntil " ", wdForward

    rng.HighlightColorIndex = wdYellow

    'Unselect the line
    rng.Select
    rng.Collapse wdCollapseStart

    Application.ScreenUpdating = True
End Sub

【讨论】:

  • 非常感谢您的帮助。但这对我不起作用。当我按箭头时,我的光标卡住了。但是,根据您帖子中给出的指示,我能够为左箭头开发解决方案。我已经用那个解决方案编辑了这个问题。但我仍然无法将其转换为右箭头。
  • @coderH 我不明白为什么不能为左右箭头分配相同的宏?如果光标在一个单词中,按下哪个无关紧要?
  • 是的,我找到了一种将左右箭头分配给宏的方法。然后我为你写的宏做了。但这对我不起作用。因为它看起来像光标卡在突出显示的单词中。它不动。不过非常感谢您的帮助。它为我指明了解决左箭头的方向。
猜你喜欢
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 2020-06-07
相关资源
最近更新 更多