文档库 最新最全的文档下载
当前位置:文档库 › VB VBA获取word光标位置、编辑区域、输入区域、菜单、工具栏、标尺、滚动条句柄

VB VBA获取word光标位置、编辑区域、输入区域、菜单、工具栏、标尺、滚动条句柄

VB VBA获取word光标位置、编辑区域、输入区域、菜单、工具栏、标尺、滚动条句柄
VB VBA获取word光标位置、编辑区域、输入区域、菜单、工具栏、标尺、滚动条句柄

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowRect Lib "user32 " (ByVal hwnd As Long, lpRect As RECT) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function ClientToScreen Lib "user32 " (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Declare Function GetCaretPos Lib "user32 " (lpPoint As POINTAPI) As Long

Private Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

Private Type POINTAPI

x As Long

y As Long

End Type

Dim MPos As POINTAPI

Dim a As RECT

Dim hw

Sub test() '不针对特殊情况,如果不能正常获取Word编辑区句柄,请重新打开word

hw = FindWindow("OpusApp", vbNullString) 'word句柄

hw = FindWindowEx(hw, 0, "_WwF", vbNullString) '类wwf句柄,从属word

hw = FindWindowEx(hw, 0, "_WwB", vbNullString) '类wwb句柄,从属wwf

hw = FindWindowEx(hw, 0, "_WwG", vbNullString) '类wwg句柄,从属wwb,即编辑框句柄

'word->_WwF(编辑区、标尺、滚动条)->_WwB(编辑区、标尺、滚动条、和审阅)->_WwG(编辑区)

'hw = FindWindowEx(hw, 0, "MsoCommandBarDock", "MsoDockTop") '从属word

'hw = FindWindowEx(hw, 0, "MsoCommandBar", "菜单栏") '从属word

GetWindowRect hw, a

编辑框顶部 = a.Top

MsgBox "编辑框顶部在屏幕的垂直位置:" & a.Top

光标所在行数 = https://www.wendangku.net/doc/e818663968.html,rmation(wdFirstCharacterLineNumber) GetCaretPos MPos

'ClientToScreen hwnd, MPos

MsgBox "光标所在编辑区的垂直位置:" & MPos.x & ", " & MPos.y '光标在屏幕中的位置

光标垂直位置 = MPos.y

Application.ActiveDocument.ActiveWindow.SmallScroll down:=2

'Set rng = ActiveWindow.RangeFromPoint(0, a.Top) '调整150这个值,150是菜单和工具栏占屏幕的像素。

' rng.Select

End Sub

相关文档