To achieve a secure and unique identification of a computer when your VBA file is opened, you can use different methods for macOS and Windows. Here are the techniques tailored for each operating system:
On Windows, you can use WMI to retrieve the BIOS Serial Number or the Disk Drive Serial Number. These are difficult to spoof and provide a unique identifier for the machine.
Here’s how you can get the BIOS Serial Number using VBA:
Sub GetBIOSSerialNumber()
Dim objWMIService As Object
Dim strComputer As String
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
For Each objItem In colItems
BIOSSerial = objItem.SerialNumber
MsgBox "BIOS Serial Number: " & BIOSSerial
On macOS, you can use the system_profiler command to get the Hardware UUID, which is a unique identifier for the Mac.
Here’s how you can execute a shell command in VBA to retrieve the Hardware UUID:
Set shell = CreateObject("WScript.Shell")
command = "system_profiler SPHardwareDataType | awk '/Hardware UUID/ {print $3}'"
result = shell.Exec("/bin/bash -c """ & command & """").StdOut.ReadAll
MsgBox "Hardware UUID: " & Trim(result)
You can combine both methods in your VBA project and determine the operating system at runtime to execute the appropriate code. Here is an example of how you might structure this:
If Application.OperatingSystem Like "*Mac*" Then
GetWindowsBIOSSerialNumber
Sub GetWindowsBIOSSerialNumber()
Dim objWMIService As Object
Dim strComputer As String
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
For Each objItem In colItems
BIOSSerial = objItem.SerialNumber
MsgBox "BIOS Serial Number: " & BIOSSerial
Set shell = CreateObject("WScript.Shell")
command = "system_profiler SPHardwareDataType | awk '/Hardware UUID/ {print $3}'"
result = shell.Exec("/bin/bash -c """ & command & """").StdOut.ReadAll
MsgBox "Hardware UUID: " & Trim(result)
- Permissions: Ensure your VBA code has the necessary permissions to execute WMI queries on Windows and shell commands on macOS.
- Spoofing: While these methods are robust, no technique is entirely spoof-proof. Physical access to the machine and advanced knowledge could potentially bypass these checks.
This solution provides a strong foundation for identifying computers uniquely across both operating systems with VBA.