Logon name

L

Lloyd

We're running Windows XP Professional on our desktops and
users logon to their computers via a Domain.

Is there a way to user VBA code to grab the user's logon
name? Multiple users login at each computer, and
the "Application.Username" property only gives me the
name listed under Tools | Options | User Information,
which can easily be changed by the user and therefore may
not represent the computer's login name.

Thanks for any suggestions.

Lloyd
 
L

Lars-Eric Gisslén

Lloyd,

Try this code:

Private Const NO_ERROR = 0

'***********************************
'* WinAPI declaration
'***********************************
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" ( _
ByVal lpszLocalName As String, _
ByVal lpszUserName As String, _
lpcchBuffer As Long) As Long


Option Explicit

Public Function LoggedOnUser() As String
Dim lpUserName As String
Dim lpnLength As Long
Dim lResult As Long

' Define the length for the string to pass to API
lpnLength = 256

' Allocate memory for the return string from API
lpUserName = Space(lpnLength)

' Call Win API to get the name of the logged on user
lResult = WNetGetUser(vbNullString, lpUserName, lpnLength)

If lResult = NO_ERROR Then
' Call function to terminate the string at first null character
LoggedOnUser = PSZ2VBString(lpUserName)
Else
LoggedOnUser = ""
End If

End Function

Public Function PSZ2VBString(pszString As String) As String
' Terminates a PSZ string at the first CHR(0) character
Dim sReturn As String
Dim iNullCharPos As Long

iNullCharPos = InStr(pszString, vbNullChar)

If iNullCharPos > 0 Then
' return everything up to the null character
sReturn = Left(pszString, iNullCharPos - 1)
Else
' no null character, return the whole string
sReturn = pszString
End If

PSZ2VBString = sReturn

End Function
 
L

Lloyd

Thanks a lot for this code, Lars-Eric.

I'm going to try this out when I get to the office today.
Your help is really appreciated, this problem has been
nagging me for several days now.

Lloyd
 
L

Lloyd

Lars-Eric,

I just want to thank you again for this code snippet.
I've just had a chance to test it out, and it ran
absolutely perfectly.

This is a major help for me and our institution.

Thanks again.

Lloyd
 
L

Lars-Eric Gisslén

Lloyd,

Glad I could help.

Using the login name can be often be handy for indifying the user. We use
the login name for a number of documents we have created for a high school.
When a student, or employee, fills in a document that requires personal
information, the form will be prefilled. In the VBA code we get the login
name for the current user and then retrieve all information for that user
from Active Directory.

--
Regards,
Lars-Eric Gisslén


"Lloyd" <[email protected]> skrev i meddelandet
Lars-Eric,

I just want to thank you again for this code snippet.
I've just had a chance to test it out, and it ran
absolutely perfectly.

This is a major help for me and our institution.

Thanks again.

Lloyd
 
L

Lloyd

That's interesting. I can think of a number of instances
where being able to get the login name will be useful in
our institution as well. I can definitely see this code
being reused many times.

Thanks again.

Lloyd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top