|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Private Declare PtrSafe Function SendMessage& Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Public Declare PtrSafe Function PostMessage Lib "user32.dll" Alias "PostMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare PtrSafe Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String) As Long
Private Declare PtrSafe Function FindWindow& Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String)
Private Declare PtrSafe Function FindWindowEx& Lib "user32.dll" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String)
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal s As Long)
Private Declare PtrSafe Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" _
(ByVal pszPath As String) As Long
Private Declare PtrSafe Function CreateFont Lib "gdi32" Alias "CreateFontA" ( _
ByVal h As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, _
ByVal W As Long, ByVal i As Long, ByVal u As Long, ByVal s As Long, _
ByVal c As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, _
ByVal PAF As Long, ByVal f As String) As Long
Sub 记事本操作() 'winapi方式
Dim hWnd&, hwndz&, hMenu&, MenuID&, a&, b&, c&, i&, j&, k%, st$
st = "C:\Users\lss\Documents\abc.txt" '文件路径/名称
Shell "notepad.exe", 1 '打开记事本/相当于新建文本文件
Sleep 1000
hWnd = FindWindow("Notepad", vbNullString) '记事本句柄
hwndz = FindWindowEx(hWnd, 0, "Edit", vbNullString) '文本编辑框句柄
'发送文本"abc"//WM_SETTEXT = &HC
SendMessage hwndz, &HC, 0, ByVal CStr("abc")
'发送换行符Chr(10)/Chr(13)/vbCrLf/vbCr/vbLf/vbNewline
SendMessage hwndz, &H102, ByVal &HD, ByVal 0
PostMessage hWnd, &H111, 4, 0 '打开另存为对话框
Sleep 1000
hwnds = FindWindow("#32770", "另存为") '获取另存为对话框句柄
b = FindWindowEx(hwnds, 0, "Button", vbNullString) '获取保存按钮句柄
ss = Array("DUIViewWndClassName", "DirectUIHWND", "FloatNotifySink", "ComboBox", "Edit")
For i = 0 To UBound(ss)
If i = 0 Then a = hwnds Else a = c
c = FindWindowEx(a, 0, ss(i), vbNullString) '获取文件名编辑框句柄
Next
SendMessage c, &HC, 0, ByVal st '设置文本WM_SETTEXT = &HC
SendMessage b, &HF5, 0, 0 '自动点击保存按钮WM_CLICK = &HF5
Sleep 1000
PostMessage hWnd, &H10, 0, 0 '关闭窗口WM_CLOSE = &H10
End If
End Sub
|
|