[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.
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
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
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
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.
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
your declaration type for ielink is incorrect, so can not work
What should it be?
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
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?
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
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
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
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
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