上下移动时突出显示Word文档的当前行 [英] highlight current line of word document when move up or down

查看:80
本文介绍了上下移动时突出显示Word文档的当前行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一种程序,使上下箭头移动时,高亮显示整个文本行.因此,当我使用箭头键向上或向下移动时,它会突出显示光标所在的行.

I wanted to develop a program such a way that when up or down arrow moved, highlight the entire line of text. So when I go up or down with arrow keys it highlight the line where my cursor is.

所以我开发了这段代码.

So I developed this code.

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分配给下箭头键.

Then I tried to assign this macro to both Up arrow key and Down arrow key. Then I realised that we can't assign one macro for 2 key combinations. So I created 2 macros like this. (Content is same. Only name is different.). And assigned SelectLineUp to Up arrow key and assigned SelectLineDown to down arrow key.

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

现在的问题是,当我按下键盘上的向下箭头时,它会按我的预期工作.但是,当我按向上箭头时,它仍然在文档中下降.非常感谢您能告诉我我做错了什么.

Now the problem is when I press down arrow in the keybord it works as I intended. But when I press Up arrow, it still goes down dirrenction in the document. Highly appreciate if you can tell me what I have done wrong.

推荐答案

以下内容对我有用.我使用了一些其他方法来更改选择"(或范围")位置,例如MoveEndMoveStartCollapse.请注意整个文档的突出显示设置的更改,因此您不必更改选择.

The following works for me. I used some additional methods for changing the Selection (or Range) locations, such as MoveEnd, MoveStart and Collapse. Note the change for the highlight setting of the entire document, so that you don't have to change the Selection.

如果使用F8单步执行代码,并在VBA编辑器和文档窗口之间切换,则可以看到这些方法的工作方式.详细信息可以在VBA帮助中找到.

If you use F8 to step through the code, and switch between the VBA Editor and document windows, you can see how these methods work. The details can be found in the VBA Help.

Sub SelectLineUp()
    Application.ScreenUpdating = False
    ActiveDocument.content.HighlightColorIndex = wdNoHighlight

    Selection.MoveEnd wdLine, -1
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend

    Selection.Range.HighlightColorIndex = wdYellow

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

Sub SelectLineDown()
    Application.ScreenUpdating = False

    ActiveDocument.content.HighlightColorIndex = wdNoHighlight

    Selection.MoveStart wdLine, 1
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend

    Selection.Range.HighlightColorIndex = wdYellow

    'Unselect the line
    Application.Selection.Collapse wdCollapseStart

    Application.ScreenUpdating = True
End Sub

这篇关于上下移动时突出显示Word文档的当前行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆