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:
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?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
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.




Reply With Quote