【问题标题】:highlight current line of word document when move up or down向上或向下移动时突出显示当前 Word 文档行
【发布时间】: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

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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    以下内容对我有用。我使用了一些额外的方法来更改选择(或范围)位置,例如MoveEndMoveStartCollapse。请注意整个文档的高亮设置的更改,这样您就不必更改选择。

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

    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
    

    【讨论】:

    • 非常感谢。你真是个天才。
    【解决方案2】:

    试试这个。这对我有用,同时保持代码非常干燥。

    Option Explicit
    
    Private Declare Function GetKeyState Lib "user32.dll" (ByVal nKey As Long) As Integer
    
    Public Sub KeyUpOrDown()
        Dim keyUp As Boolean
        keyUp = CBool(GetKeyState(vbKeyUp) And &H80) ' Was "keyup" pressed
    
        If (keyUp) Then
            Selection.MoveUp Unit:=wdLine
            Call HighlightLine
        Else
            Selection.MoveDown Unit:=wdLine
            Call HighlightLine
        End If
    
    End Sub
    
    Private Sub HighlightLine()
        Application.ScreenUpdating = False
        Dim currPosition As Range
        Set currPosition = Selection.Range
    
        ActiveDocument.Content.HighlightColorIndex = wdNoHighlight
        Selection.Expand Unit:=wdLine
        Selection.Range.HighlightColorIndex = wdYellow
    
        currPosition.Select
    
    End Sub
    

    两个触发键都可以绑定到公共子例程“KeyUpOrDown”。

    我喜欢这种工作方式,因为它具有原生的感觉。正如您在代码中暗示的那样,选择点不会折叠到一侧,但在切换行时会保持其原始位置。

    另一个大事件是使用外部“user32.dll”库模拟按键事件。

    我希望你会发现它有用。 谢谢。

    【讨论】:

    • 对不起,我不知道这部分代码。 {Private Declare Function GetKeyState Lib "user32.dll" (ByVal nKey As Long) As Integer } 我们不能将其修改为 { (Private Declare Function GetKeyState Lib "user64.dll" (ByVal nKey As Long) As Integer } 才能工作64位?
    • 还有什么好的网站或 youtube 视频可以了解 {Private Declare Function GetKeyState Lib "user32.dll" (ByVal nKey As Long) As Integer} 之类的东西吗?
    • @coderH 将 Declare 语句替换为:Declare PtrSafe Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer 将适用于 64 位和 32 位
    • 您可以查看此webpage 以了解有关 64 位和 32 位兼容性的更多信息。但是要了解有关“声明语句”的更多信息,您可以搜索此关键字“在 VBA 中声明语句”。我建议您在查看 MSDN 之前阅读博客文章。也请查看cpearson post。 Cpearson 通常是学习高级 VBA 的好网站。
    【解决方案3】:

    在接受的答案中有一点性能改进。如果您有 400 页的书,则取消突出显示整个文档会花费太多时间。
    此外,代码不会将您返回到相同的光标位置。 我稍微修改了代码以解决这两个问题:
    将此添加到声明部分。

    Dim currSelection As Range
    


    将这些宏绑定到向上和向下键

    Sub UpKey()
        Application.ScreenUpdating = False
        'get current position
        Dim currPosition As Range
        Set currPosition = Selection.Range
    
        'remove highlight from previous line
        If Not currSelection Is Nothing Then
            currSelection.HighlightColorIndex = wdNoHighlight
        End If
            
        'move and highlight new line
        Selection.MoveUp Unit:=wdLine
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Set currSelection = Selection.Range
        Selection.Range.HighlightColorIndex = wdYellow
    
        'get back to old spot and move the line
        currPosition.Select
        Selection.MoveUp Unit:=wdLine
        
        Application.ScreenUpdating = True
    End Sub
    
    Sub DownKey()
     Application.ScreenUpdating = False
        Dim currPosition As Range
        Set currPosition = Selection.Range
    
        If Not currSelection Is Nothing Then
            currSelection.HighlightColorIndex = wdNoHighlight
        End If
            
        Selection.MoveDown Unit:=wdLine
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Set currSelection = Selection.Range
        Selection.Range.HighlightColorIndex = wdYellow
    
        currPosition.Select
        Selection.MoveDown Unit:=wdLine
        
        Application.ScreenUpdating = True
    End Sub
    


    注意:如果由于某种原因某行保持突出显示,则将光标移至该行并向上或向下移动一次。

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 2010-12-19
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多