Results 1 to 11 of 11

Thread: [RESOLVED] [Excel VBA]Internet Explorer Download Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    Resolved [RESOLVED] [Excel VBA]Internet Explorer Download Problem

    I've been putting a Google Finance stock history downloader together from all the cool tips I've received from this forum and have almost everything working just as I need.

    Here is my code:

    Code:
    Option Explicit
    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 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 SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Const BM_CLICK = &HF5
    
    Sub GetData()
        Const DownloadTitle As String = "File Download"
        Const DownloadClass As String = "#32770"
        Const ChildTitle    As String = "&Open"
        Const ChildClass    As String = "Button"
        Dim hwndParent As Long
        Dim hwndChild  As Long
        Dim lpClassName As String
        Dim RetVal As Long
         
        Dim sURL As String
        Dim IeApp As InternetExplorer
        Dim IeDoc As HTMLDocument
        Dim IeECol As IHTMLElementCollection
        Dim IeLink As HTMLLinkElement
         
        Set IeApp = New InternetExplorer
        sURL = "http://www.google.com/finance/historical?q=NYSE:IBM#"
         
        With IeApp
            .Visible = True
            .Navigate sURL
            Do Until .Busy = False And .ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
        End With
             
        Set IeDoc = IeApp.Document
        Set IeECol = IeDoc.getElementsByTagName("A")
             
        For Each IeLink In IeECol
            If IeLink.innerText = "Download to spreadsheet " Then
                IeLink.Click 'this results in a new IE window being opened with the download dialog
                     
                Do Until IeApp.Busy = False And IeApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
                         
                'find the new download window
                hwndParent = FindWindow(DownloadClass, DownloadTitle)
                         
                'make sure we've got a valid handle
                If hwndParent <> 0 Then
                    'this download dialog has a child window
                    hwndChild = FindWindowEx(hwndParent, 0&, ChildClass, ChildTitle)
                             
                    'make sure we've got a valid handle
                    If hwndChild <> 0 Then
                        'make sure the window is active in order to send the click message
                        RetVal = SetForegroundWindow(hwndParent)
                        DoEvents
                        SendMessage hwndChild, BM_CLICK, 0, ByVal 0&
                        DoEvents
                    Else
                        MsgBox "Can't locate the button on the download dialog!"
                        Exit For
                    End If
                Else
                    MsgBox "Download window not found!"
                End If
                        
                Exit For
            End If
        Next IeLink
        IeApp.Quit
                 
        Set IeApp = Nothing
        Set IeDoc = Nothing
        Set IeECol = Nothing
        Set IeLink = Nothing
    End Sub
    The problem is with the line "IeLink.Click". I can actually see the new IE window being opened but then it immediately goes away. If I manually intervene by pausing the script here and clicking the link in person, I get the new window with the expected download dialog box. If I then resume the code, everything works as planned. So why isn't "IeLink.Click" working?

    Before you suggest just going directly to the download URL, please be advised that I'm doing it the hard way because it is training ground for something very similar for which I will not have direct file download access.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel VBA]Internet Explorer Download Problem

    why not skip all the preliminary and just go directly to the link for download spreadsheet
    else read the link and navigate there like
    vb Code:
    1. If ielink.innerText = "Download to spreadsheet " Then
    2.         ieApp.navigate2 ielink.href
    this then only require finding the dialog in the current browser window
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    Re: [Excel VBA]Internet Explorer Download Problem

    I tried that. Didn't work. File is attached if you can see what I am doing wrong.
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel VBA]Internet Explorer Download Problem

    Didn't work.
    what did not work? did it error , do nothing? wrong result?
    when it tested, it certainly opened the dialog box, but i did not try past that

    edit: i downloaded your file here and got error just trying to open the site ("operation aborted"), it was working on a different computer last night, so i don't know, at this point, what the issue is, i will try again tonight on the other computer again
    Last edited by westconn1; Jan 31st, 2012 at 03:29 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    Re: [Excel VBA]Internet Explorer Download Problem

    Did nothing... same as when I was trying to do the IeLink.Click.

    I do know one thing about this code is that it will not run on IE9 - has to be IE8 or lower because in IE9 the new download notification bar is not actually a child window nor are its controls so you have to test on IE8 or less.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel VBA]Internet Explorer Download Problem

    your declaration type for ielink is incorrect, so can not work
    your loop after navigating to to the download will be endless as it will require the dialog to be closed to be not busy and readystate =4
    the send message selects a button but does not click
    seems to select cancel the first time then open on trying again
    Last edited by westconn1; Feb 1st, 2012 at 04:54 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    Re: [Excel VBA]Internet Explorer Download Problem

    Quote Originally Posted by westconn1 View Post
    your declaration type for ielink is incorrect, so can not work
    What should it be?

    Quote Originally Posted by westconn1 View Post
    your loop after navigating to to the download will be endless as it will require the dialog to be closed to be not busy and readystate =4
    I'm no guru but I thought that DO UNTIL in the following statement would wait until IE navigation was finished (when .Busy is FALSE and READYSTATE is 4).

    HTML Code:
    Do Until .Busy = False And .ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
    Quote Originally Posted by westconn1 View Post
    the send message selects a button but does not click
    seems to select cancel the first time then open on trying again
    I can't tell if that is what you are experiencing or if you are stating that the code is wrong. If the latter is the case, then what should the following code be changed to?

    Code:
    SendMessage hwndChild, BM_CLICK, 0, ByVal 0&

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel VBA]Internet Explorer Download Problem

    What should it be?
    htmlanchorelement
    I'm no guru but I thought that DO UNTIL in the following statement would wait until IE navigation
    navigation pauses while dialog is displayed
    [QUOTE]I can't tell if that is what you are experiencing or if you are stating that the code is wrong./QUOTE]my experience, so i assume the code needs to be modified
    then what should the following code be changed to?
    you will have te research more on send message to dialog, after fixing the other problems you will be able to tell if you get the same results as me

    note: on one computer the code works fine up until the dialog is displayed, but on another i get an error immediately after navigation
    Attached Images Attached Images  
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel VBA]Internet Explorer Download Problem

    try like this, i spent some considerable time to find out how to make this work
    vb Code:
    1. IeApp.Navigate IeLink.href
    2. Do Until hwnd
    3.   hwnd = FindWindow(vbNullString, "File Download")
    4.   DoEvents
    5. Loop
    6. Do Until hchildwnd
    7.   hchildwnd = FindWindowEx(hwnd, ByVal 0&, vbNullString, "&Open")
    8.   DoEvents
    9. Loop
    10. RetVal = SetForegroundWindow(hwnd)
    11. Application.Wait (Now + TimeValue("0:00:02")) ' this value may need to be adjusted
    12. ' alternatively sleep could be used
    13. SendMessage hchildwnd, BM_CLICK, 0, 0
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    Re: [Excel VBA]Internet Explorer Download Problem

    That did it and the code makes sense, especially since we're not actually trying to work with the original instance of IE. Thanks a lot!

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] [Excel VBA]Internet Explorer Download Problem

    all the findwindow /sendmessage code could be in a separate procedure in a module, which can be called after clicking the link
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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