我想以这样的方式开发一个程序,当按下向右或向左箭头时,当前单词会突出显示。因此,当我用箭头键向左或向右走时,光标所在的单词会突出显示。我可以利用其中的信息来开发其中的一部分 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
但是我仍然无法弄清楚如何将其转换为右箭头。如果存在类似-的字符,则失败:()({}
答案 0 :(得分:0)
您可以使用一个宏执行此操作-可以将一个宏分配给多个键。
建议的方法使用MoveStartUntil
和MoveEndUntil
方法扩展范围(或选择),直到遇到指定的字符集(在这种情况下为空格)。
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
答案 1 :(得分:0)
这适用于向右箭头,请检查一下。
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
看起来很多,但效果很好,尤其是在段落中断处。希望您可以为左箭头进行修改。