Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mac Address

Status
Not open for further replies.

PercyN

Programmer
May 16, 2007
86
Does anyone know how to retrieve the Mac (physical) address of a computer through code?

Thanks
 
How about:

Code:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top