User Names


Posted by Rob Jackson on July 30, 2001 8:18 AM

I need to get the Windows User name. The User Name from Excel is not necessarily correct but we are running terminal server and so the Windows User name will be. Can anyone advise.

Thanks in advance for any help.

Rob.

Posted by Jerid on July 30, 2001 10:22 AM

Rob, this is what I use.

Add this code to a module.

'Win32 API Functions - Used by the NTDomainUserName Procedure
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


Public Function NTDomainUserName() As String
On Error GoTo ErrHandler

Dim sDllFound As String

sDllFound = Dir("C:\WINNT\System32\advapi32.dll")

If sDllFound <> vbNullString Then
Dim strBuffer As String * 255
Dim lngBufferLength As Long
Dim lngRet As Long
Dim strTemp As String

lngBufferLength = 255
lngRet = GetUserName(strBuffer, lngBufferLength)
strTemp = UCase(Trim(strBuffer))

NTDomainUserName = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1)
Exit Function
End If

NTDomainUserName = vbNullString

ExitHandler:
Exit Function

ErrHandler:
MsgBox (Err.Number & vbCrLf & Err.Description)
Resume Next

End Function

Jerid



Posted by Rob Jackson on July 31, 2001 12:26 AM

sDllFound = Dir("C:\WINNT\System32\advapi32.dll") If sDllFound <> vbNullString Then Dim strBuffer As String * 255 Dim lngBufferLength As Long Dim lngRet As Long Dim strTemp As String lngBufferLength = 255 lngRet = GetUserName(strBuffer, lngBufferLength) strTemp = UCase(Trim(strBuffer)) NTDomainUserName = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1) Exit Function End If NTDomainUserName = vbNullString

Cheers Jerid, works a treat.

Rob