Checking an active internet connection

I wrote a small application that accesses a handful of search websites and puts the results in a text document that runs several hundred times a day.

It saves individual search results in several local folders, so the next time you search for these words, it captures them locally rather than loading the website again.

This works great, though not fast. People are impressed because until a few weeks ago they did it manually, literally downloading six different search sites, doing a search, and then copying and pasting the results into a text document.

However, our online office is unreliable and does not work for the last half day. This means that about 400 bad searches were saved in local folders and pasted into the resulting documents.

When a person searched, they could tell if the Internet was broken, and they would do their searches later. Obviously, however, this application cannot say, because I do not use the API or anything else, and because I am limited to using the VBA environment (I’m not even allowed with MZ tools), I need to find a way to make sure The Internet works until the flow of the program continues, without relying on too many links and, preferably, without scripting for the phrase "404 Page Not Found".

I am not very familiar with VB, and VBA is ruining me in many ways, so there probably is an easy way to do this, so I ask here.

Appreciate any help.

+7
vba ms-office networking
source share
7 answers

You can use the MSXML library and use the XMLHttpRequest class to check things

eg.

On Error Resume Next Dim request As MSXML2.XMLHTTP60 request.Open "http://www.google.com" request.Send Msgbox request.Status 

The status will provide you with an HTTP status code of what happened to the request. You may need to do a few more checks, depending on your scenario.

Hope this helps.

+6
source share

Obviously your problem has many levels. You should start by defining “connected to the Internet” and continue developing backup strategies that include not writing invalid files on failure.

Regarding the question "I'm connected," you can try using the Win32 API:

 Private Declare Function InternetGetConnectedState Lib "wininet.dll" _ (ByRef dwflags As Long, ByVal dwReserved As Long ) As Long Public Function GetInternetConnectedState() As Boolean GetInternetConnectedState = InternetGetConnectedState(0&,0&) End Function 

Although depending on your network configuration (proxy / NAT / firewall restrictions, etc.), Windows may have a different opinion about this than you.

Trying to GET the pages that interest you, checking the return status in the HTTP headers (gateway timeout, 404, what you expect when it "doesn't work") can also be a way.

+22
source share

Use the following code to test your internet connection first anable XML v6.0 in your links

 Function checkInternetConnection() As Integer 'code to check for internet connection 'by Daniel Isoje On Error Resume Next checkInternetConnection = False Dim objSvrHTTP As ServerXMLHTTP Dim varProjectID, varCatID, strT As String Set objSvrHTTP = New ServerXMLHTTP objSvrHTTP.Open "GET", "http://www.google.com" objSvrHTTP.setRequestHeader "Accept", "application/xml" objSvrHTTP.setRequestHeader "Content-Type", "application/xml" objSvrHTTP.Send strT If err = 0 Then checkInternetConnection = True Else MsgBox "Internet connection not estableshed: " & err.Description & "", 64, "Additt !" End If End Function 
+3
source share

Unfortunately, this is a bit of a difficult question to answer for several reasons:

  • How do you determine a broken Internet connection? Are you checking a valid IP address? Did you call How do you know that you have permission to verify these things? How do you know that a firewall / antivirus computer is not inconvenient?
  • Once you have established that the connection is working, what do you do if the connection drops in the middle of the operation?

There are probably ways to do what you want to do, but many things like the "dash in detail" usually appear. Do you have any way to verify that a saved search is valid? If so, this may be the best way to do this.

+2
source share

Based on shakalpesh's answer and comments on it, there are (at least) two ways to get a web page in Word without parsing the XML returned by the XMLHTTP60 object.

(NB, an HTTP status code of 200 indicates that the "request succeeded" - see here )

  • write XMLHTTP60.ResponseText to a text file and then call Documents.Open in that text file
 If (xhr.Status = 200) Then hOutFile = FreeFile Open "C:\foo.html" For Output As #hOutFile Print #hOutFile, xhr.responseText Close #hOutFile End If // ... Documents.Open "C:\foo.html" 

This has the disadvantage that some related items may be lost and you will receive a message box when you open the file

  • check the status of the url with the XMLHTTP60 object and then use Documents.Open to open the url still:
 If (xhr.Status = 200) Then Documents.Open "http://foo.bar.com/index.html" End If 

There is little chance that the XMLHTTP60 request may succeed, and Documents.Open fail (or vice versa). Hope this should be a rather unusual event, though

+2
source share

I found most of the answers here and elsewhere are confusing or incomplete, so here's how to do it for idiots like me:

 'paste this code in at the top of your module (it will not work elsewhere) Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef dwflags As Long, ByVal dwReserved As Long) As Long Private Const INTERNET_CONNECTION_MODEM As Long = &H1 Private Const INTERNET_CONNECTION_LAN As Long = &H2 Private Const INTERNET_CONNECTION_PROXY As Long = &H4 Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20 'paste this code in anywhere Function IsInternetConnected() As Boolean Dim L As Long Dim R As Long R = InternetGetConnectedState(L, 0&) If R = 0 Then IsInternetConnected = False Else If R <= 4 Then IsInternetConnected = True Else IsInternetConnected = False End If End Function 'your main function/calling function would look something like this Private Sub btnInternetFunction_Click() If IsInternetConnected() = True Then MsgBox ("You are connected to the Internet") 'code to execute Internet-required function here Else MsgBox ("You are not connected to the Internet or there is an issue with your Internet connection.") End If End Sub 
0
source share

Where

 Private Const INTERNET_CONNECTION_MODEM As Long = &H1 Private Const INTERNET_CONNECTION_LAN As Long = &H2 Private Const INTERNET_CONNECTION_PROXY As Long = &H4 Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20 

used in the code above?

Because R = InternetGetConnectedState (L, 0 &) returns true when connected to Wi-Fi that does not have internet

0
source share

All Articles