WarPigl3t
Well-known Member
- Joined
- May 25, 2014
- Messages
- 1,611
Hey everyone. Quick question. I have the below function which will return the MAC address of the computer being used. I was just running some tests when I realized my MAC address was "" or blank in other words. I thought about it and realized that my internet went down and I disconnected from the network while I was working on my excel project. So then I thought, could not being connected to the internet be affecting the macro from returning my MAC address? So I connected back to the internet to test my theory. Then when I ran my macro, it did return my MAC address. So my question is, why is my MAC address being returned by the below function only when I am connected to the internet? A MAC address is like the address of your house. It doesn't change and being connected to the internet has nothing to do with a MAC address. So why is my function returning blank when I'm not connected to the internet?
Code:
Function GetMACAddress() As String
Dim objVMI As Object
Dim vAdptr As Variant
Dim objAdptr As Object
Dim adptrCnt As Long
Dim adres As String
adres = ""
Set objVMI = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set vAdptr = objVMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdptr In vAdptr
If Not IsNull(objAdptr.MACAddress) And IsArray(objAdptr.IPAddress) Then
For adptrCnt = 0 To UBound(objAdptr.IPAddress)
If Not objAdptr.IPAddress(adptrCnt) = "0.0.0.0" Then
GetNetworkConnectionMACAddress = objAdptr.MACAddress
Exit For
End If
Next adptrCnt
[COLOR=#008000] 'MsgBox "Your MAC Address is: " & GetNetworkConnectionMACAddress[/COLOR]
adres = GetNetworkConnectionMACAddress
End If
Next
GetMACAddress = adres
End Function