[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.
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:
If ielink.innerText = "Download to spreadsheet " Then
ieApp.navigate2 ielink.href
this then only require finding the dialog in the current browser window
1 Attachment(s)
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.
Re: [Excel VBA]Internet Explorer Download Problem
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
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.
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
Re: [Excel VBA]Internet Explorer Download Problem
Quote:
Originally Posted by
westconn1
your declaration type for ielink is incorrect, so can not work
What should it be?
Quote:
Originally Posted by
westconn1
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
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&
1 Attachment(s)
Re: [Excel VBA]Internet Explorer Download Problem
Quote:
What should it be?
htmlanchorelement
Quote:
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
Quote:
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
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:
IeApp.Navigate IeLink.href
Do Until hwnd
hwnd = FindWindow(vbNullString, "File Download")
DoEvents
Loop
Do Until hchildwnd
hchildwnd = FindWindowEx(hwnd, ByVal 0&, vbNullString, "&Open")
DoEvents
Loop
RetVal = SetForegroundWindow(hwnd)
Application.Wait (Now + TimeValue("0:00:02")) ' this value may need to be adjusted
' alternatively sleep could be used
SendMessage hchildwnd, BM_CLICK, 0, 0
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!
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