Results 1 to 2 of 2

Thread: [Resolved]Get handle to windows dialog and enter text in box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Posts
    100

    [Resolved]Get handle to windows dialog and enter text in box

    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
    Last edited by darbid; Jun 25th, 2009 at 02:21 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Posts
    100

    Re: Get handle to windows dialog and enter text in box

    I like posting in the VB Forum cause I then find the answer.

    Code:
    Call SetWindowText(wnd2, ByVal "hello")
    Does not work that is for the Title of the window for exmple.

    Code:
    Call SendMessage(wnd2, WM_SETTEXT, Len("I Did it"), "I Did it")
    However works.

    I am happy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width