Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


read HTML from WEB

Posted on 1997-12-20
Medium Priority
Last Modified: 2013-11-13
How can I read in Excel or VBA a HTML page from an URL addres into a File for later use?
Question by:thoba
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3

Expert Comment

ID: 1446246
If you have the internet transfer control that ships with VB5.  If not I can provide code that would do it with a winsock control.  Do you have access to either of these?

Author Comment

ID: 1446247
Thank You
I have access to both winsock control and internet transfer control.
Do You have code for using it in VBA with excel ?

Accepted Solution

rantanen earned 100 total points
ID: 1446248
Unfortunately I don't have here Excel installed so I can't really test this with it, but following should work (maybe after some small modifications)

Make following declarations

Const icHostResolvingHost = 1 'The control is looking up the IP address of the specified host computer.
Const icHostResolved = 2   'The control successfully found the IP address of the specified host computer.
Const icConnecting = 3     ' control is connecting to the host computer.
Const icConnected = 4  'The control successfully connected to the host computer.
Const icRequesting = 5     'The control is sending a request to the host computer.
Const icRequestSent = 6    'The control successfully sent the request.
Const icReceivingResponse = 7  'The control is receiving a response from the host computer.
Const icResponseReceived = 8   'The control successfully received a response from the host computer.
Const icDisconnecting = 9  'The control is disconnecting from the host computer.
Const icDisconnected = 10  'The control successfully disconnected from the host computer.
Const icError = 11 'An error occurred in communicating with the host computer.
Const icResponseCompleted = 12 'The request has completed and all data has been received.

Const icUnknown = 0    'Unknown.
Const icDefault = 1    'Default protocol.
Const icFTP = 2    'FTP. 'File Transfer Protocol.
Const icReserved = 3   'Reserved for future use.
Const icHTTP = 4   'HTTP. HyperText Transfer Protocol.
Const icHTTPS = 5  'Secure HTTP.

Const icString = 0     'Default. Retrieves data as string.
Const icByteArray = 1  'Retrieves data as a byte array.


Add following code and implied controls

Private Sub btnGetHTML_Click()
    Inet1.Protocol = icHTTP
    Inet1.Execute txtURL.Text, "GET"
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
    Dim vtData As Variant
    Dim intFile As Integer

    intFile = FreeFile()
    Select Case State
        ' Add other cases to follow what is taking place
        Case icHostResolvingHost
            StatusBar1.Panels(1).Text = "Connecting..."
        Case icConnected
            StatusBar1.Panels(1).Text = "Connected..."
        Case icReceivingResponse
            StatusBar1.Panels(1).Text = "Receiving..."
        Case icError
            StatusBar1.Panels(1).Text = "Error in data transfer"
            Exit Sub
        Case icResponseCompleted
            ' Open a file to write to.
            Open "c:\HTMLtest.txt" For Binary Access Write As #intFile
            vtData = Inet1.GetChunk(1024, icString)
            Do While LenB(vtData) > 0
                Put #intFile, , vtData
                vtData = Inet1.GetChunk(1024, icString)
            Put #intFile, , vtData
            Close #intFile
            StatusBar1.Panels(1).Text = "Ready"
    End Select
End Sub


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 1446249
thoba, all you need is this:

   Dim HTML$

   Inet1.AccessType = icDirect 'SET THIS ACCORDINGLY
   Inet1.URL = ""
   HTML$ = Inet1.OpenURL
   Inet1.Cancel 'That's it, You've got what you need.

   Open "HTML PAGE.txt" for output as #1
   Print #1, HTML$
   Close 1


Expert Comment

ID: 1446250
Unfortunately, I don't think you can use the INet control in Excel unless you're using Excel 97 - and then I'm not sure because I'm running Excel 95 (7.0).  Excel 95 has no means of adding the INet component.  You can create an app in VB with the above code and shell the exe from Excel using the Shell Function.  Check out "Shell" in Excel help.


Expert Comment

ID: 1446251
mrmick, I know that is the simplest solution what you provided. It is however restricted to material in text form (of course that is enough if you want to get only the source of the HTML page), but if you want to get also the binary material (eg. images on the page) you have to do what I showed, so in that sense it is more general.

But if thoba is more satisfied with your answer, that is OK with me.

Expert Comment

ID: 1446252
Images referenced in an html document must be requested separately by the client after the initial page is received.  This allows a client (such as a browser) to be configured not to automatically receive image information when the data connection is slow.  No binary data is sent by the http server when a html page is requested.


Expert Comment

ID: 1446253
I know that, but if you want to request it, then...

Another thing which might mean something if you are on a slow connection and retrieving quite a large file is the operation mode.

OpenURL is synchronous and Execute is asynchronousand can thus be made to run in the background.

Expert Comment

ID: 1446254
True - the OpenURL is synchronous.  But wouldn't this be another argument as to why the OpenURL is the preferred method?

I'm assuming the only purpose of the VB app is to get the html data and save it to a file... at which point the app would terminate... which would be the signal to the Excel procedure that the file is ready.


Author Comment

ID: 1446255
Is there no possibility to create an object by program with the createObject() function? It's OK for me to read only text.

Author Comment

ID: 1446256
I have now a solution. I read the htm File with a Java application called with the shell() function, which generates a local File.

Thank You for help

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question