|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Macro to login to horseracebase.com web site and download today cards file
Hi,
Now i am in need of help... I have to login to a website, using a username and password, then go to a specific page and download a file with today cards data. I never done this before, so any advice will be precious. The site is: http://www.horseracebase.com/horse-racing-today.php The file needed is downloaded from the RED ( ) download button. The question is: how can i push the red button from excel? Thanks in advance, Catalin |
#2
|
||||
|
||||
I've done this before, Catalin, and it's complicated. Actually it isn't the VBA code that's complicated, but navigating your way through other people's web pages usually is.
Internally, a web page is a mass of objects containing objects containing objects. A very ordinary web page can contain thousands of objects to 15 or 20 levels, and figuring out how they're organized take much more effort as a programmer than just finding the red button with your eyes once it's displayed—so much more effort that I eventually wrote an Excel routine to "map" a web page's object tree so that I can easily (fairly easily) find the ones I'm interested in and figure out how to get to them in an Excel program.... --- I wrote the above this morning. Next I was going to say "I ran that utility on your racing page, and I've attached a map of the various nodes so you can see how it's set up". But this is the first time I've run this particular VBA program since I switched to my new computer, and it's failing at the point where it tries to recurse through the various ChildNodes in the Document object. My program navigates correctly to the URL, and it's reading the page alright—at least I can see it in IE. But although my program can see that there are objects in the Document object, the object Watch window claims that there are "No Variables" in the Document. I spent some time fooling around with it, but so far I don't see what's I'm doing wrong. Naturally I suspect it's because I have to do something different with the new object either under Window 7 or (more likely) IE 10, but so far I haven't found the right combination of search terms to google the solution. You can always read the raw HTML code of a page (at least I used to be able to; I hope I still can ), but to push the button you need the DOM, the Document Object Model. I'll keep looking, and after all it may work for you. The object type in VBA is "SHDocVw.InternetExplorer", if you don't mind googling for it; and there's an interesting looking article at http://support.microsoft.com/kb/176792 that talks about how to connect to an instance of IE that's already running, which may help too. Meanwhile I'll have to figure out what changed before I can help anyone else with it. |
#3
|
|||
|
|||
Quote:
Here is my suggestion I do something very similar at work everyday but Excel can sometimes not run the code to do this easily. I have done this in the past with sendkeys. Most people will reply this and say Booo to sendkeys and I will agree. sendkeys is not the most dependable. Try looking into autohotkey and write a script to click on the screen at a location type in the user info and then click on the red button. The syntax is pretty straight forward and I know it does not really apply to this form but I believe it would work very well with what you are trying to do. |
#4
|
|||
|
|||
Well, after some attempts, still no valid file.
The code: (in 2 versions) Code:
Sub DownloadFile() Dim myURL As String myURL = "http://horseracebase.com/excellcards.php" Dim WinHttpReq As Object Set WinHttpReq = CreateObject("Microsoft.XMLHTTP") WinHttpReq.Open "POST", myURL, False, ".........", "........." WinHttpReq.Send myURL = WinHttpReq.responseBody If WinHttpReq.Status = 200 Then Set oStream = CreateObject("ADODB.Stream") oStream.Open oStream.Type = 1 oStream.Write WinHttpReq.responseBody oStream.SaveToFile ThisWorkbook.Path & "\cards " & Format(Time, "hh-mm") & ".xls", 2 oStream.Close End If End Sub Sub DownloadFile2() Dim myURL As String myURL = "http://horseracebase.com/excellcards.php" Dim req As Object Dim user As String, pass As String user = "........." pass = "........." credentials = "username=" & user & "&password=" & pass Set req = CreateObject("WinHttp.WinHttpRequest.5.1") req.SetAutoLogonPolicy 0 req.Open "POST", myURL, False req.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" req.Send credentials myURL = req.responseBody If req.Status = 200 Then Set oStream = CreateObject("ADODB.Stream") oStream.Open oStream.Type = 1 oStream.Write req.responseBody oStream.SaveToFile "E:\cards.xls", 2 ' 1 = no overwrite, 2 = overwrite oStream.Close End If End Sub " <p class=standard>You must hold a valid <a href='index.php'>HorseRaceBase</a> membership to access this feature. <a href='subscribe.php'>Sign up for a free trial now</a></p> Can you point to the source of this error? I have to say i could not found the error, i used Fiddler to analyze browsing session, no result... |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Create a SharePoint site macro | Fredrik | Misc | 0 | 10-25-2012 12:49 AM |
Pop-up requesting me to download a file when | Gillonline | Outlook | 0 | 04-18-2012 01:33 AM |
Outlook forces file download to browser | Choja | Outlook | 0 | 11-17-2010 01:19 AM |
Macro to download FTP File | HowardC | Excel | 0 | 05-31-2010 11:16 AM |
Trying to download a .doc file from email or website on word 2007 on vista | wilson723 | Word | 1 | 10-23-2009 11:17 AM |