PercyN
Programmer
- May 16, 2007
- 86
Does anyone know how to retrieve the Mac (physical) address of a computer through code?
Thanks
Thanks
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Sub Test()
'Method #1
Debug.Print echoMAC(Environ("ComputerName"))
'Method #2
Debug.Print getMACaddress(Environ("ComputerName"))
End Sub
' Function returns MAC address of strcomputer
Function getMACaddress(strComputer)
'[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1449560&page=3[/URL]
Dim objExecObject, strText, searchstring1, searchstring2, position1, position2
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c nbtstat -a " & strComputer)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
searchstring1 = "MAC Address ="
position1 = InStr(1, strText, searchstring1, 0) + 13
getMACaddress = Mid(strText, position1, 18)
Loop
End Function
Function echoMAC(strComputer)
'[URL unfurl="true"]http://www.webdeveloper.com/forum/showthread.php?t=115694[/URL]
On Error Resume Next
' strComputer = (InputBox(" Computer name for MAC address", "Computer Name"))
If strComputer <> "" Then
strInput = True
End If
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem In colItems
Debug.Print objItem.MACAddress
Next
End Function