章鱼哥—VB.NET Office操作之Word(二)

这篇文章在 章鱼哥—VB.NET Office操作之Word(一) 的基础上,给类添加了光标操作的内容,包括获取word 文档中光标的位置,将光标跳至指定行,上下左右移动光标等操作。该文中给定的方法直接复制到上篇文中的类中即可。不懂得可以联系我。下篇文章将给出这个类具体实现的代码,所有代码都经过笔者的测试,可直接使用。

 

'*********************************************************************
'作者:章鱼哥,QQ:3107073263 群:309816713    
'如有疑问或好的建议请联系我,大家一起进步  
'********************************************************************* 
 '获取当前的光标位置信息,存放在数组中
    Public Function GetCursor() As ArrayList
        Try
            Dim cursor As New ArrayList
            '当前光标所在的页数
            Dim Page As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
            '当前光标所在行数
            Dim row As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdFirstCharacterLineNumber)
            '当前光标所在列数
            Dim cul As Object = ZDocument.Application.Selection.Information(Word.WdInformation.wdFirstCharacterColumnNumber)
            cursor.AddRange({Page, row, cul})
            Return cursor
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function

    '鼠标定位到指定页
    Public Sub GoToPage(ByVal Page As Integer)
        Try
            '跳转到指定页码
            ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToFirst, Page)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '光标调到指定行。这个是绝对跳转
    Public Sub GoToAbsolutLine(ByVal Row As Integer)
        Try
            '跳转到指定行,说明:这个行是相对于整个文档来算的,将如第一页就2行,你跳到第三行的时候,就是第2页的第1行
            '读者可自行测试,目前还实现不了给定页,行,列调到精确位置的功能。至少我还没实现。这里就不进行实现了
            ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToFirst, Row)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '光标调到指定行。这个是相对跳转。大家应该理解什么意思的
    Public Sub GoToOppsiteLine(ByVal Row As Int16)
        Try

            '读者可自行测试,目前还实现不了给定页,行,列调到精确位置的功能。至少我还没实现
            If Row >= 0 Then '如果大于0,像后跳转
                ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToNext, Math.Abs(Row))
            Else '小于0,像前跳转
                ZDocument.Application.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToPrevious, Math.Abs(Row))
            End If


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '左移光标
    Public Sub MoveLeft()
        ZDocument.Application.Selection.MoveLeft() '每次移动1位
    End Sub
    '右移
    Public Sub MoveRight()
        ZDocument.Application.Selection.MoveRight() '每次移动1位
    End Sub
    '上移
    Public Sub MoveUp()
        ZDocument.Application.Selection.MoveUp() '每次移动1位
    End Sub
    '下移
    Public Sub MoveDown()
        ZDocument.Application.Selection.MoveDown() '每次移动1位
    End Sub

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。