Tuesday 17 April 2018

How to get full name of windows user VBA | VBA code to get windows user full name



How to get full name of windows user VBA :

Function GetWinUsers_Fullname()
Dim objSystemInfo As Object
Dim strLDAPName As String
 
Set objSystemInfo = CreateObject("ADSystemInfo")
strLDAPName = objSystemInfo.UserName
Set objSystemInfo = Nothing

GetWinUsers_Fullname = GetUserName(strLDAPName)
End Function

Function GetUserName(strLDAPName)
  Dim objUserName As Object
  Dim strFullName As String
  Dim arrLDAPName() As String
  Dim intIndex As Integer
 
  On Error Resume Next
  strFullName = ""
  Set objUserName = GetObject("LDAP://" & strLDAPName)
  If Err.Number = 0 Then
    strFullName = objUserName.Get("givenName") & Chr(32) & objUserName.Get("sn")
  End If
  If Err.Number <> 0 Then
    arrLDAPName = Split(strLDAPName, ",")
    For intIndex = 0 To UBound(arrLDAPName)
      If UCase(Left(arrLDAPName(intIndex), 3)) = "CN=" Then
        strFullName = Trim(Mid(arrLDAPName(intIndex), 4))
      End If
    Next
  End If
  Set objUserName = Nothing
 
  GetUserName = strFullName
 
End Function

No comments:

Post a Comment