我目前正在制作一张提取MAC地址的Excel表格。我设法使用以下代码提取连接/在线MAC地址:
Dim objVMI As Object
Dim vAdptr As Variant
Dim objAdptr As Object
Dim adptrCnt As Long
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
End If
Next
Range("A1").Value = GetNetworkConnectionMACAddress
但是,此代码仅提取连接的MAC。我可以知道如何提取离线MAC地址吗?
答案 0 :(得分:2)
Public Function getMacAddress()
'== function to get MAC adress (all)
Dim objVMI As Object, vAdptr As Variant
Dim objAdptr As Object
Dim strMacAdr As String, i As Long
getMacAddress = vbNullString
Set objVMI = GetObject("winmgmts:\\" & "." & "\root\cimv2")
'== get the using MAC address with IP
Set vAdptr = objVMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdptr In vAdptr
If IsNull(objAdptr.MACAddress) Then GoTo ff
strMacAdr = Trim(objAdptr.MACAddress)
If Not IsArray(objAdptr.IPAddress) Then GoTo ff
For i = 0 To UBound(objAdptr.IPAddress)
If Not Trim(objAdptr.IPAddress(i)) = "0.0.0.0" Then getMacAddress = getMacAddress & vbCrLf & strMacAdr & " (" & Trim(objAdptr.IPAddress(i)) & ")"
Next
ff:
Next
'== get all MAC address
Set vAdptr = objVMI.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL")
For Each objAdptr In vAdptr
If Not IsNull(objAdptr.MACAddress) Then
strMacAdr = Trim(objAdptr.MACAddress)
If InStrRev(getMacAddress, strMacAdr) = 0 Then getMacAddress = getMacAddress & vbCrLf & Trim(objAdptr.MACAddress)
End If
Next
Set objVMI = Nothing
Set vAdptr = Nothing
Set objAdptr = Nothing
MsgBox getMacAddress
End Function