Capturing html source as txt file

  • Thread starter Thread starter Tok
  • Start date Start date
T

Tok

I am using the ActiveX "Web Browser Control" in a form.

I can navigate to a URL in the forms VB module with:
Me.WebBrowser0.Navigate "www.mysite.com"

Once the URL is loaded, in VB I want to do the equivilent of View/Source and
then SaveAs to text file.

Is this possible?

Thank you!
Tok
 
Thank you for the reply. I sort of figured it out. However, I'm stumped as
to why my code errors out as a certain point when I'm writing the html string
to a file.

My code actually works fine for the websites I'm navigating to, however when
testing it on www.google.com, it would error out at one particular character
and I can't figure out why.

If you uncomment the "On Error Resume Next" line, the code will complete and
write the whole html source code except for that one character, which happens
to be a question mark ("?").

If anyone out there wants to test it out and be able to figure out why it
crashes at character 636 in the string and let me know, I would be greatful!

Of course this is time sensitive. Google.com may change their website html
at anytime, so this may not apply in the near future.

The code was built using:
Access 2003
A new form with an active X control "Microsoft Web Browser" named
"WebBrowser0"

Paste the following code in the form:

Option Compare Database
Option Explicit

Private Sub Form_Load()

Me.WebBrowser0.Navigate "www.google.com"

End Sub

Private Sub WebBrowser0_DocumentComplete(ByVal pDisp As Object, URL As
Variant)

'When webpage is finished loading,
'write the html source code to a log file
WriteLog Me.WebBrowser0.Document.body.innerHTML, True

End Sub

Private Sub WriteLog(HTMLsource As String, Optional bolOverwrite As Boolean
= True)

Dim fs As Variant
Dim fname As String
Dim logfile As Variant
Dim i As Long

'Put log file in same dir as database
fname = Application.CurrentProject.path & "\log.txt"

'Set file system object
Set fs = CreateObject("Scripting.FileSystemObject")

'Delete the log file if it already exists
If bolOverwrite Then fs.DeleteFile (fname)

'Create or open the log file for appending
Set logfile = fs.OpenTextFile(fname, 8, True)

'Write to the log file one character at a time
'On Error Resume Next
For i = 1 To Len(HTMLsource)
logfile.Write Mid(HTMLsource, i, 1)
Next i

'Alternate Write (write the whole string at once)
'logfile.WriteLine HTMLsource

'Close the log file
logfile.Close

'Clean up
Set fs = Nothing
Set logfile = Nothing

End Sub
 
Back
Top