Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Created August 10, 2012 02:42
Show Gist options
  • Save dck-jp/3310512 to your computer and use it in GitHub Desktop.
Save dck-jp/3310512 to your computer and use it in GitHub Desktop.
Get MAC Address @ VBA
Public Function GetMacAddress()
Dim objNetwork As Object
Dim strNetworkSql As String
Dim strMacAdr As String
strNetworkSql = "SELECT * FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL"
For Each objNetwork In GetObject("winmgmts:").ExecQuery(strNetworkSql)
strMacAdr = objNetwork.MACAddress
If strMacAdr <> "" Then Exit For
Next
GetMacAddress = strMacAdr
End Function
@hh-lohmann
Copy link

That's just picking the first MAC address it finds - today you'll have at least some virtual network adapters (Windows "prefab" VPN and other tunneling, e.g. ISATAP, Teredo, 6to4), so it's very optimistic that your actual phyical adapter is retrieved as first. Though that might be the case on most computers, you should not rely on that when deploying software, i.e. running on arbitrary computers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment