|
摸索了很长时间,请教了守柔版主,用api达到了目的,代码如下:
Const VK_1 = 49
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Const MOUSEEVENTF_MOVE = &H1 '移动鼠标
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' 模拟鼠标左键按下
Private Const MOUSEEVENTF_LEFTUP = &H4 '模拟鼠标左键抬起
Private Const MOUSEEVENTF_RIGHTDOWN = &H8 '模拟鼠标右键按下
Private Const MOUSEEVENTF_RIGHTUP = &H10 ' 模拟鼠标右键抬起
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20 '模拟鼠标中键按下
Private Const MOUSEEVENTF_MIDDLEUP = &H40 '模拟鼠标中键抬起
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 '标示是否采用绝对坐标
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal Y As Long) As Long
Private Type POINTAPI
x As Long
Y As Long
End Type
Private Type MOUSEINPUT
dx As Long
dy As Long
mouseData As Long
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
uMsg As Long
wParamL As Integer
wParamH As Integer
End Type
Private Type GENERALINPUT
dwType As Long
xi(0 To 23) As Byte
End Type
Private Sub SendKey(bKey As Byte)
Dim GInput(0 To 1) As GENERALINPUT
Dim KInput As KEYBDINPUT
KInput.wVk = bKey 'the key we're going to press
KInput.dwFlags = 0 'press the key
'copy the structure into the input array's buffer.
GInput(0).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
'do the same as above, but for releasing the key
KInput.wVk = bKey ' the key we're going to realease
KInput.dwFlags = KEYEVENTF_KEYUP ' release the key
GInput(1).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
'send the input now
Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub
Private Sub Document_Open()
ss = Me.Name
ss = Split(Me.Name, ".")(0) & " - Microsoft Word"
Dim hWnd1 As Long
hWnd1 = FindWindow(vbNullString, ss) '"SciCalc"
Dim PP As POINTAPI
PP.x = 96
PP.Y = 515
SetCursorPos PP.x, PP.Y
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0&, 0&
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0&, 0&
SendKey VK_1
End Sub
使用方法:只需将此代码复制到目标文档的Thisdocument下即可。每次打开自动折叠为1级。
测试附件:
Doc1.rar
(14.8 KB, 下载次数: 33)
|
|