VBA‎ > ‎

VBA code to detect windows user id

 
Here's the code:
 
Public Function GetLDAPUserName() As String
Dim SI, colItems As Variant
Dim pos As Integer

On Error GoTo tryalternate
Set SI = CreateObject("ADSystemInfo")
pos = InStr(SI.UserName, ",")
GetLDAPUserName = Mid(SI.UserName, 4, pos - 4)
On Error GoTo 0
Exit Function
tryalternate:
Set SI = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = SI.ExecQuery("Select * From Win32_ComputerSystem")
For Each objItem In colItems
    GetLDAPUserName = objItem.UserName
Next
On Error GoTo 0
End Function
 
Comments