Check network connection with Excel VBA

Asked

Viewed 1,135 times

1

I have a macro that saves on the network three spreadsheets of a PDF file. Loop and verify which spreadsheets will be saved in PDF, and save correctly. Macro hangs or gives error when network is bad or crashed, needing to use ESC key.

How to check before if the network is active and operational?

If the network is mapped as units, logically not finding is because the network is inactive or the path does not exist.

Use On Error Resume Next one line before saving in PDF, and right after If Err.Number > 0

Dim CaminhoArq As String
Dim NomeArq As String
CaminhoArq = ActiveSheet.Range("F2").Value
NomeArq = ActiveSheet.Range("F4").Value
If Right(CaminhoArq, 1) <> "\" Then
    CaminhoArq = CaminhoArq & "\"
    On Error Resume Next
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                    Filename:=CaminhoArq & NomeArq & ".pdf", _
                                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                                    IgnorePrintAreas:=False, OpenAfterPublish:=False
    If Err.Number > 0 Then
        MsgBox "Caminho da pasta não existe!", vbExclamation, "AAAAA"
        Exit Sub
    End If
End If

If the path does not exist the error message is immediate.

But existing the path and the network being inactive or weak, it was to give immediate error and fall in IF Err.Number, but Excel hangs and does not respond, only typing ESC if it can stop the macro.

A code I found on the Internet, I’d like to know if it works for both the network and the Internet.

And continues the code only if the network is good: being bad or with some problem or inactive, for the code.

Private Declare PtrSafe Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long

Public Function IsInternetConnected() As Boolean
    IsInternetConnected = InternetGetConnectedStateEx(0, "", 254, 0)
End Function

Sub TestandoConexão()
    If IsInternetConnected = True Then
        MsgBox "Continua o código"
    Else
        MsgBox "Código interrompido"
    End If
End Sub
  • I performed tests with both codes, the first works perfectly and I couldn’t simulate a weak connection. I connected to a router with weak signal (1 wireless signal bar) and even so saved successfully, only took longer. In the Second Code, it works only when there is no connection to the network, can be connected to the wireless router and then remove the internet cable and function IsInternetConnected will return True

  • Grateful for the test. I already suspected it was for the internet, not for the network. Maybe a DIR with the path will check if the network is OK. I couldn’t get a code in Excel VBA that actually checks the network, and a code like that is what I would need. Maybe a ping code. Does it exist?

  • On the contrary, it checks the network. If the network is without internet, however the computer connected to it, the function returns True. It only returns False if no network is connected.

  • Let’s see if I understand: Regardless of whether the internet is active or not; - If you have network the function returns True. - If it has no network the function returns False. It just does not check the ping, that is, it does not check whether the network is weak or not.

  • Thank you! Es I am suspicious that I myself am making storm in a glass of water.

  • You can use: DIR or FSO (Filesystemobject) https://www.rondebruin.nl/win/s9/win003.htm

  • Or another code that I can’t post because it exceeds the number of characters allowed in the comments.

  • I have no idea how to do that!

  • There is a FAQ here, but there is no tutorial on how to answer. Just write your answer in the window at the bottom of the page, at: His Response and then click Post your answer. Check how we should format questions and answers? and then accepted the answer. I know it sounds boring, but it’s not a waste of time. It’s for the good of the community and so is the functioning of it, generate knowledge.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.