PDA

View Full Version : Solved: IE Download to Excel through VBA



myers_co
01-25-2012, 05:02 AM
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:

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?

Crocus Crow
01-25-2012, 08:59 AM
For Google Finance Historical data, all the IE automation and Windows API code is unnecessary because Excel can retrieve it directly via File - Open and specifying the 'Download to spreadsheet' URL (for example http://www.google.com/finance/historical?q=NYSE:IBM&output=csv).

Here is the code generated by the macro recorder doing exactly that:


Sub Macro1()
Workbooks.OpenText Filename:= _
"http://www.google.com/finance/historical?q=NYSE:IBM&output=csv", origin:=- _
535, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), TrailingMinusNumbers:=True
End Sub

myers_co
01-25-2012, 10:13 AM
For Google Finance Historical data, all the IE automation and Windows API code is unnecessary because Excel can retrieve it directly via File - Open and specifying the 'Download to spreadsheet' URL (for example http://www.google.com/finance/historical?q=NYSE:IBM&output=csv).

I'm aware of that. However, 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. Do you have any idea on how to overcome the click problem?

myers_co
01-31-2012, 09:29 AM
I've also tried to do a direct navigate:

If IeLink.innerText = "Download to spreadsheet " Then
IeApp.Navigate IeLink.href


That doesn't work either. I've attached the file if anyone can figure out why it isn't working.

myers_co
02-18-2012, 10:14 AM
Thanks to WESTCONN1 on VBFORUMS here is the answer for those who might be following in my footsteps:



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 HTMLAnchorElement


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


IeApp.Navigate IeLink.href


' wait for the new instance of IE pops up
Do Until hwndParent



hwndParent = FindWindow(vbNullString, DownloadTitle)

DoEvents



Loop



' get the download dialog

Do Until hwndChild

hwndChild = FindWindowEx(hwndParent, ByVal 0&, vbNullString, ChildTitle)

DoEvents



Loop



' make sure we're sitting on the parent
RetVal = SetForegroundWindow(hwndParent)


' wait for the dust to settle - could use SLEEP as alternative
Application.Wait (Now + TimeValue("0:00:02")) ' may need tweeking


' now click on the download dialog
SendMessage hwndChild, BM_CLICK, 0, 0


End If



Next IeLink

IeApp.Quit
Set IeApp = Nothing
Set IeDoc = Nothing
Set IeECol = Nothing
Set IeLink = Nothing


End Sub