VBA:WIndows API:如何分配文件路径以打开Windows? [英] VBA:WIndows API :How to assign file path to open windows?

查看:126
本文介绍了VBA:WIndows API:如何分配文件路径以打开Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用发送键

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 i found only, how to find the my "Choose File to Upload" windows and click Open button, Now i don't know how to insert filename path to file name Edit text-box in open windows. Kindly tell me how to set the text value of an 'edit' control text box with the SendMessage() function.

找到我的IE文件浏览资源管理器"的附件屏幕截图.

Find attached screenshot for My IE file browse explorer.

下面是我引用的VBA编码供参考:

Below my referred VBA coding FYI:

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

推荐答案

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

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

这篇关于VBA:WIndows API:如何分配文件路径以打开Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆