' http://randoltech.blogspot.com/2015/05/vbscript-to-check-wireless-or-wired.html
'
' modified by arnelgp for VBA use.
'
Public Function chkConnectionType() As String
'==================================================================
'
' NAME: Check Connection Type
'
' AUTHOR: Mark Randol
' DATE : 4/29/2015
'
' COMMENT: this script will return
' 1 if the both are in use (shouldn't happen, but can)
' 2 if wired LAN adapter is in use
' 3 if wireless LAN adapter is in use
' 4 if none of the LAN adapters are in use.
'==================================================================
On Error Resume Next
Dim strComputer
Dim objWMIService
Dim colWiFi
Dim colLAN
Dim objWifi
Dim objLAN
Dim state
Dim wireStatus
Dim wifiStatus
Dim strOut
Dim intOutput
'arnelgp
Dim strReturn As String
'===================================================================================
' Initialize Variables
'===================================================================================
intOutput = 4
state = ""
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colLAN = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionStatus = 2 and PhysicalAdapter = 'True'")
'==================================================================
' Enumerate the wired adapters in WMI. Add 1 to output if wired adapter is in use.
'==================================================================
'WScript.Echo ("Output starting at " & intOutput)
'Debug.Print ("Output starting at " & intOutput)
For Each objLAN In colLAN
strOut = objLAN.NetConnectionID & " " & objLAN.Name & " " & objLAN.PhysicalAdapter
If InStr(LCase(objLAN.Name), "virtual") = 0 And InStr(LCase(objLAN.Name), "multiplex") = 0 And InStr(LCase(objLAN.Name), "bridge") = 0 Then
'==================================================================
' Above line (if statement) is there to eliminate other extraneous adapters that
' still show up even though we are eliminating all but "physical" adapters. Some
' virtual adapters are still there, Microsoft being the biggest offender.
' Add to the line if necessary to remove other non-physical adapters.
'==================================================================
If InStr(LCase(objLAN.NetConnectionID), "wireless") > 0 Or InStr(LCase(objLAN.NetConnectionID), "wi-fi") > 0 Then
intOutput = intOutput - 2
'WScript.Echo (strOut & " connected. Output is now " & intOutput)
'Debug.Print (strOut & " connected. Output is now " & intOutput)
strReturn = "wi-fi"
End If
If InStr(LCase(objLAN.NetConnectionID), "wireless") = 0 And InStr(LCase(objLAN.NetConnectionID), "wi-fi") = 0 Then
intOutput = intOutput - 1
'WScript.Echo (strOut & " connected. Output is now " & intOutput)
'Debug.Print (strOut & " connected. Output is now " & intOutput)
strReturn = "wired"
End If
End If
Next
'WScript.Echo ("Final Output = " & intOutput)
'WScript.Echo (intOutput)
'WScript.Quit (intOutput)
'Debug.Print ("Final Output = " & intOutput)
'Debug.Print (intOutput)
chkConnectionType = strReturn
End Function