-2

在我尝试发送密钥问题 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
4

1 回答 1

2

我找到了解决方案 VBA程序找到打开窗口为“选择要上传的文件”,在IE浏览器中上传文件时会在IE中弹出该窗口。它选择文件名编辑框并分配文件的完整文件路径并自动单击打开按钮。

Public Declare PtrSafe Function SendMessageByString Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare PtrSafe 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 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


Dim strBuff As String, ButCap As String
Public Const WM_SETTEXT = &HC
Public Const BM_CLICK = &HF5

Sub Sample()
hw = FindWindow(vbNullString, "Choose File to Upload")
op = FindWindowEx(hw, 0&, "Button", vbNullString)

 strBuff = String(GetWindowTextLength(op) + 1, Chr$(0))
 GetWindowText op, strBuff, Len(strBuff)
 ButCap = strBuff

    Do While op <> 0

                If InStr(1, ButCap, "Open") Then
                   OpenRet = op
                    Exit Do
                End If

Loop

hw1 = FindWindowEx(hw, 0&, "ComboBoxEx32", vbNullString)

hw2 = FindWindowEx(hw1, 0&, "ComboBox", vbNullString)

hw3 = FindWindowEx(hw2, 0&, "Edit", vbNullString)

Call SendMessageByString(hw3, WM_SETTEXT, 0, "C:\Users\kkath\Documents\mine\QC\2015\MAY\Email1.pdf")
Call SendMessage(OpenRet, BM_CLICK, 0, 0)
End Sub
于 2016-01-18T16:00:15.647 回答