Extract the Page Source of URL as a String using VBA

Hi,

I am trying to extract the HTML Page Source of a given URL as a String. I found the code below which works but for some reason doesn't work all the time (it gives various different errors sporadically).

Public Function gsGetPageSource(URLToCrawl As String) As String
Dim ie As Object

Set ie = CreateObject("InternetExplorer.Application")

With ie
.Navigate URLToCrawl
Do While .Busy And Not .ReadyState = 4
DoEvents
Loop

gsGetPageSource = IIf(StrComp(.Document.Title, "Cannot find server", vbTextCompare) = 0, vbNullString, .Document.Body.outerHTML)
.Quit
End With

Set ie = Nothing

End Function

Open in new window

xyobiAsked:
Who is Participating?
 
thydzikCommented:
try the attached;
Sub tester()

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objHTTP = CreateObject("Msxml2.XMLHTTP")
 
    objHTTP.Open "GET", "http://google.com", False
    objHTTP.send
    strPageText = objHTTP.responseText
    MsgBox strPageText
End Sub

Open in new window



grab-html.xls
0
 
Nico BontenbalCommented:
See comment 35465535 in this question:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26977101.html
You'll need the function GetInternetResult in the Postalcode module of the attached file. You can call this function for example like this:
?GetInternetResult("www.experts-exchange.com","Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26977101.html","")

0
 
xyobiAuthor Commented:
thydzik... you are a legend, thanks so much for your help

nicobo... thanks for your solution, I tried it out and it looked interesting but im not sure if it does exactly as i want as i dont have the skills required to make the necessary changes
0
 
xyobiAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.