Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-04-2013, 09:33 AM
Catalin.B Catalin.B is offline Macro to login to horseracebase.com web site and download today cards file Windows Vista Macro to login to horseracebase.com web site and download today cards file Office 2010 32bit
Expert
Macro to login to horseracebase.com web site and download today cards file
 
Join Date: May 2011
Location: Iaşi, România
Posts: 386
Catalin.B is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 10-04-2013, 03:16 PM
BobBridges's Avatar
BobBridges BobBridges is offline Macro to login to horseracebase.com web site and download today cards file Windows 7 64bit Macro to login to horseracebase.com web site and download today cards file Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

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.
Reply With Quote
  #3  
Old 10-04-2013, 08:46 PM
excelledsoftware excelledsoftware is offline Macro to login to horseracebase.com web site and download today cards file Windows 7 64bit Macro to login to horseracebase.com web site and download today cards file Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Quote:
Originally Posted by Catalin.B View Post
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
Catalin,

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.
Reply With Quote
  #4  
Old 10-20-2013, 10:02 AM
Catalin.B Catalin.B is offline Macro to login to horseracebase.com web site and download today cards file Windows Vista Macro to login to horseracebase.com web site and download today cards file Office 2010 32bit
Expert
Macro to login to horseracebase.com web site and download today cards file
 
Join Date: May 2011
Location: Iaşi, România
Posts: 386
Catalin.B is on a distinguished road
Default

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
Both codes downloads the file but the problem is that the code is not actually logging in, the cards file downloaded has this message inside:
" <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...
Reply With Quote
Reply

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
Macro to login to horseracebase.com web site and download today cards file Trying to download a .doc file from email or website on word 2007 on vista wilson723 Word 1 10-23-2009 11:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft