I am automating Internet Explorer.
Ther is a file upload with a browse button and I am trying to get a handle on the Dialog, enter the path and click on the open button.
I do not have SPY++ available to me. So I have found this code below on the net.
I can click on the button YEH ...so I know that I have the right dialog.
wnd2 is not null so I have something, but no text appears.
APIs are complicated for me so if someone could please have a look I would appreciate it a lot.
Code:'declares the imported API functions that are used in this example: 'http://en.wikipedia.org/wiki/API 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 GetCurrentProcessId Lib "kernel32" () As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long Private Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long 'declares a constant that's used by PostMessage()... this specific message is 'ButtonMessage_CLICK (ie: message sent to click on the button) Private Const BM_CLICK = &HF5 Public Sub getthewindow(ByRef mywb As InternetExplorer, ByVal mywbHWND As Long) Dim wnd1 As Long Dim wnd2 As Long Dim wnd3 As Long 'find the dialog-window ("#32770" is the classname for it).. the 'dialog-window is the window where you choose a file to Open/Save 'wnd1 = FindWindow("#32770", vbNullString) wnd1 = FindWindow("#32770", "Choose file") If (wnd1 <> 0) Then 'if it's found, check it's parent-window to make sure it's our 'form's window.. the dialog-window will be a child of our form.. 'this is not the case for all similar situations If (GetParent(wnd1) = mywb.hwnd) Then 'now.. we're going to find the ComboBox where we normally 'type a file in at. normally, a ComboBox has an Edit control 'within it where the text is entered (such as a TextBox).. 'FindWindowEx is similar to FindWindow(), but it allows us to 'search for windows within a parent 'find the main ComboBox window.. there normally isn't an '*Ex32 version of the control but there was in my case.. wnd2 = FindWindowEx(wnd1, 0, "ComboBoxEx32", vbNullString) 'find the child-window of that ("ComboBox") wnd2 = FindWindowEx(wnd2, 0, "ComboBox", vbNullString) 'find the child-window of that ("Edit") wnd2 = FindWindowEx(wnd2, 0, "Edit", vbNullString) 'find the button ("&Open").. we'll need to click this! wnd3 = FindWindowEx(wnd1, 0, "Button", "&Open") 'if the "Edit" and "Button" windows were found... If ((wnd2 <> 0) And (wnd3 <> 0)) Then Call SetActiveWindow(wnd1) 'set the text of the "Edit" to "c:\test.html" Call SetWindowText(wnd2, ByVal "hello") 'click the "&Open" button Call PostMessage(wnd3, BM_CLICK, 0, 0) End If End If End If End Sub




Reply With Quote