【发布时间】:2021-02-24 18:19:29
【问题描述】:
我想开发一个程序,当向上或向下箭头移动时,突出显示整行文本。因此,当我使用箭头键向上或向下时,它会突出显示光标所在的行。
所以我开发了这段代码。
Application.ScreenUpdating = False
Dim currentPosition As Range
Set currentPosition = Selection.Range 'pick up current cursor position
Selection.WholeStory
Selection.Range.HighlightColorIndex = wdNoHighlight
currentPosition.Select 'return cursor to original position
Selection.Range.HighlightColorIndex = wdYellow
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Range.HighlightColorIndex = wdYellow
'Unselect the line
Application.Selection.EndOf
Application.ScreenUpdating = True
然后我尝试将此宏分配给向上箭头键和向下箭头键。然后我意识到我们不能为 2 个组合键分配一个宏。所以我创建了 2 个这样的宏。 (内容相同。只是名称不同。)。 并将 SelectLineUp 分配给向上箭头键并将 SelectLineDown 分配给向下箭头键。
Sub SelectLineUp()
Application.ScreenUpdating = False
Dim currentPosition As Range
Set currentPosition = Selection.Range 'pick up current cursor position
Selection.WholeStory
Selection.Range.HighlightColorIndex = wdNoHighlight
currentPosition.Select 'return cursor to original position
Selection.Range.HighlightColorIndex = wdYellow
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Range.HighlightColorIndex = wdYellow
'Unselect the line
Application.Selection.EndOf
Application.ScreenUpdating = True
End Sub
这是向下箭头
Sub SelectLineDown()
Application.ScreenUpdating = False
Dim currentPosition As Range
Set currentPosition = Selection.Range 'pick up current cursor position
Selection.WholeStory
Selection.Range.HighlightColorIndex = wdNoHighlight
currentPosition.Select 'return cursor to original position
Selection.Range.HighlightColorIndex = wdYellow
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Range.HighlightColorIndex = wdYellow
'Unselect the line
Application.Selection.EndOf
Application.ScreenUpdating = True
End Sub
现在的问题是,当我按下键盘中的向下箭头时,它会按我的预期工作。但是当我按向上箭头时,它仍然会在文档中向下移动。非常感谢您能告诉我我做错了什么。
【问题讨论】: