# Accessing the Web via VB6 (Automation Error!)



## philipp (Jan 20, 2004)

Hi.
I am trying to read a website into a variable so I can run parsing scripts in Visual Basic 6.0

I am using:

Dim Web As New SHDocVw.InternetExplorer 
Dim Doc As New MSHTML.HTMLDocument 

Web.Navigate "http:// website " 
Set Doc = Web.Document 

(I have references and instances of the following classes installed on my form)
- Microsoft Internet Controls (shdocvw.dll) 
- Microsoft HTML Object Library (MSHTML.TLB) 

When I run the above code, I get an Automation Error for the last line.
What am I doing wrong ???

Help appreciated.
Thanks
Phil.


----------



## coderitr (Oct 12, 2003)

I found something quite by accident. I placed the code you posted into a new form in VB in a button click event procedure. If I just click the button with no breakpoints, I get the same error you did. If I set a breakpoint on the line that's generating the error and wait a couple of seconds before running it, it goes right through. It appears that the navigate method takes a couple of seconds to set up the document object. I have no idea what that means.


----------



## coderitr (Oct 12, 2003)

Welcome to TSG by the way.


----------



## philipp (Jan 20, 2004)

Coderitr,
I have noticed that too, its driving me nuts !

Im trying to teach myself VB, but it seems someone up there has other ideas.

Thanks for sharing my pain anyway.
Phil.


----------



## ciener (Jan 31, 2004)

you can use two controls one being winsocket or inet.

Here is winsock

Winsock1.Connect "www.google.com", 80

Private Sub Winsock1_Connect()
'Once the winsock has connected to the server we send our request.
'GET tells the server we are GETting the html
'the / just tells it to send the default page, usually index.html, .htm, .php or what have you.
'The HTTP/1.0 tells it that we are using HTTP version 1
'Then we let the server know it has the whole request by sending 2 vbCrLf's

Winsock1.SendData "GET / HTTP/1.0" & vbCrLf & vbCrLf

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
'Once the server starts returning data we need to handle it.
'As some pages can be large they will be returned in several packets.
'So we need a buffer to store all the packets in. So ....

Static buf As String

'Now we tell the Winsock to make the packet into a string called Wdata

Winsock1.GetData Wdata, vbString

'We add the packets to the buffer.

buf = buf & Wdata

'We need to check once all the html has arrived.
'So we check the buffer for the closing HTML tag.
'As some sites use capitals for tags and others use lowercase we use Lcase.
'This makes the buffer lowercase while we check it.
'If it's there we call the Parse Sub routine and pass the buffer to it.
If InStr(1, LCase(buf), "") Then Parse (buf)

Private Sub Parse(Data As String)
'In here we are going to get the page title from the html.
'We them use the title for the forms caption.
'The integers a and b are used for finding the start and end of the title.

Dim a, b As Integer

'We no longer need the connection so we close winsock.

Winsock1.Close

'Now we find the title of the page.
'We make a = the start of the title by locating the tag.
'We then add the length of the tag to it as the title starts
'at the end of the tag not the start.

a = InStr(1, LCase(Data), "") + 7

'Then using a as the starting point we make b = the end of the title by locating .

b = InStr(a, LCase(Data), "")

'Now we make the title of the page the form's caption.
'The title is in the string called Data.
'It starts at a and it's length is b - a.

Form1.Caption = Mid(Data, a, b - a)

'Now we display all the returned data in our text box.
'The information at the start are the headers.
'These tell use the server type, the time and date on the server, etc.

Text1.Text = Data

End Sub

thats one way you can do that.

The other way is inet = Internet transfer control

test = Inet1.OpenURL("www.google.com")
Text1.Text = test

thats all the code.. much easer to use this.

Good luck hope this helps.


----------



## philipp (Jan 20, 2004)

Ciener, 
Thanks for that.

The Inet option works well.

Thanks to all.
Phil.


----------

