Link to home
Start Free TrialLog in
Avatar of xyobi
xyobi

asked on

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

Avatar of Nico Bontenbal
Nico Bontenbal
Flag of Netherlands image

See comment 35465535 in this question:
https://www.experts-exchange.com/questions/26977101/Get-External-Web-Result-into-Excel.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("https://www.experts-exchange.com","Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26977101.html","")

ASKER CERTIFIED SOLUTION
Avatar of Travis Hydzik
Travis Hydzik
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xyobi
xyobi

ASKER

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
Avatar of xyobi

ASKER

thanks