在我尝试发送密钥问题 1之前,我在上传自动化方面苦苦挣扎,在我决定使用 Windows API 程序之后,我在网站上引用了很多代码,我只找到了,如何找到我的“选择要上传的文件”窗口并单击打开按钮,现在我不知道如何在打开的窗口中将文件名路径插入文件名编辑文本框。请告诉我如何使用 SendMessage() 函数设置“编辑”控件文本框的文本值。
查找我的 IE 文件浏览器的附加屏幕截图。
下面我提到的 VBA 编码仅供参考:
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 GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias _
"GetWindowTextLengthA" (ByVal hwnd As Long) 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
Const BM_CLICK = &HF5&
Dim Ret As Long, ChildRet As Long, OpenRet As Long
Dim strBuff As String, ButCap As String
Sub Sample()
'~~> Get the handle of the "File Download" Window
Ret = FindWindow(vbNullString, "Choose File to Upload")
If Ret <> 0 Then
MsgBox "Main Window Found"
'~~> Get the handle of the Button's "Window"
ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
'~~> Check if we found it or not
If ChildRet <> 0 Then
MsgBox "Child Window Found"
'~~> Get the caption of the child window
strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
GetWindowText ChildRet, strBuff, Len(strBuff)
ButCap = strBuff
'~~> Loop through all child windows
Do While ChildRet <> 0
'~~> Check if the caption has the word "Open"
'~~> For "Save" or "Cancel", replace "Open" with
'~~> "Save" or "Cancel"
If InStr(1, ButCap, "Open") Then
'~~> If this is the button we are looking for then exit
OpenRet = ChildRet
Exit Do
End If
'~~> Get the handle of the next child window
ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString)
'~~> Get the caption of the child window
strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
GetWindowText ChildRet, strBuff, Len(strBuff)
ButCap = strBuff
Loop
'~~> Check if we found it or not
If OpenRet <> 0 Then
MsgBox "The Handle of Open Button is : " & OpenRet
'~~> Click the button using Send Message
SendMessage OpenRet, BM_CLICK, 0, 0
Else
MsgBox "The Handle of Open Button was not found"
End If
Else
MsgBox "Child Window Not Found"
End If
Else
MsgBox "Window Not Found"
End If
End Sub