VBA:WIndows API :How to assign file path to open windows?

前端 未结 1 934
旧巷少年郎
旧巷少年郎 2021-01-29 12:48

I am struggling in upload automation before i have tried with send key Question 1 after my question i have decided go with Windows API program and i refer lot of code in website

1条回答
  •  礼貌的吻别
    2021-01-29 13:26

    I Have found solution VBA program find Open window as "Choose File to Upload" , this window will popup in IE while uploading document in IE browser.It select file name edit box and assign full file path of file and click open button automatically.

    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
    

    0 讨论(0)
提交回复
热议问题