Excel/VBA - Abort script if network connection does not exist

前端 未结 3 1004
無奈伤痛
無奈伤痛 2021-01-23 11:19

Are there any VBA code to look for a present internet connection?

I have a code that will run on a timer. This code will open a file on a local network share drive.

3条回答
  •  耶瑟儿~
    2021-01-23 11:39

    I usually use the code below to determine if a network connection exits. The function returns true or false.

    Declaration:

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

    Function:

    Public Function IsInternetConnected() As Boolean
    Dim strConnType As String
    Dim lngReturnStatus As Long
    IsInternetConnected = False
    lngReturnStatus = InternetGetConnectedStateEx(lngReturnStatus, strConnType, 254, 0)
    If lngReturnStatus = 1 Then IsInternetConnected = True
    End Function
    

    Using the function in a Sub:

     If IsInternetConnected = False Then
        output = MsgBox("No Network Connection Detected!", vbExclamation, "No Connection")
    Else
        Do stuff that requires the network connection
    End If
    

提交回复
热议问题