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


Handling Inet Errors in a Loop

Posted on 1999-07-23
Medium Priority
Last Modified: 2008-02-01

  I need help in handling Inet when retreiving web pages in a loop. Some URLs cause Inet to hang because it cannot connect and my loop just stalls :( On Error Resume Next is no help because once it resumes it just races through my URLs without saving them to file. HELP!

                Open App.Path & "\links.dat" For Input As #1
                  Do While Not EOF(1)
                  Line Input #1, strURL
                Open App.Path i & ".htm" For Output As #2
                  strFILE = Inet1.OpenURL(strURL, 0)
                  Print #2, strFILE
                  Close #2
                Close #1
Question by:James
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
  • 2
  • 2

Author Comment

ID: 1526884
Edited text of question.

Expert Comment

ID: 1526885
'Try this, after the line below.....

'Open App.Path i & ".htm" For Output As #2
'use these lines

Do While Inet1.StillExecuting = True


If Len(strFILE) > 1 Then
Print #2, strFILE
Close #2
Close #2
End If


Close #1


Author Comment

ID: 1526886

 same thing. It seems it just can't get itself out of the loop and on to the next url from the url file. You can trap for the errors and say On Error GoTo Line: but for some reason passing a strURL gets all screwed up and nothing really happens after it continus from next line. This is really odd but I am sure it has to do with the fact I am in a Loop.

Here is a bad url that will cause unspeakable confsion to inet if you want to observe this behaviour first hand, place on top of some good url links in a file and run the code.


Accepted Solution

Biffo earned 800 total points
ID: 1526887
Here is what the problem is....

Inet is a piece of ****, it is best used for retrieving known working single url's, such as your own that you know is valid. The guy's at MS who build this crap never use the stuff in the real world under real world conditions LOL They test it under ideal conditions using idea url addresses <snicker>

The gist of your problem has to do with whoever built the Inet control at MS, they thought it be cute to have it triger not one, but two friggin error events. This makes handling errors impossible for all intent and  purposes. So, when you do a "On Error Resume Next" it responds to one error event, to yet another following the first one -- right after another and it just get's compounded in a loop. Real messy business.

A more stable way of doing what you want is to use the wininet stuff. First, get the wininet API from the following address:

It comes as vbinet.exe I think. Add that API module (wininet.bas) to your code.

Now just modifiy your code to resemble this:

Private Sub Whatever_it_is()

Open App.Path & "\links.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, strURL

Call Do_WinInet

Close #1
End Sub

Private Sub Do_WinInet()
On Error Resume Next
    Dim hOpen               As Long
    Dim hOpenUrl            As Long
    Dim sUrl                As String
    Dim bDoLoop             As Boolean
    Dim bRet                As Boolean
    Dim sReadBuffer         As String * 2048
    Dim lNumberOfBytesRead  As Long
    Dim sBuffer             As String
    Dim iRetVal             As Integer

   hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
   'InternetSetStatusCallback hOpen, AddressOf MyCallBack
   hOpenUrl = InternetOpenUrl(hOpen, urlname, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
    bDoLoop = True
    While bDoLoop
    sReadBuffer = vbNullString
    bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
        sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
        If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
 Open App.Path i & ".htm" For Binary Access Write As #2

    Put #2, , sBuffer
    Close #2

    If hOpen <> 0 Then InternetCloseHandle (hOpen)
    If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)

End Sub

This runs much better under VB5 then VB6 since VB6 has a lot of problems dealing with async issues and threading. Under VB6 your app will be completely unavailable till the downloading is complete. Under VB5 it is a little more forgiving, plus, you can run it as ASYNC if you want to put some effort into it. I run it this way with no problems. In VB6 forget it.

Expert Comment

ID: 1526888
One more thing, the line below I used just the name urlname to signify whatever the name of the url string you are using, in your case it is strURL, so replace urlname with that. I wasn't paying attention when I wrote this.

hOpenUrl = InternetOpenUrl(hOpen, strURL, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 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