query system username

  • Thread starter Thread starter Kevin Perez
  • Start date Start date
K

Kevin Perez

Hello,

How can I return the system username either in VBA or a
function?

I've written a macro to enable several different users to
input data. I want to provide a measure of security so
that only select users can activate the macro.

Thanks.


Kevin
 
Kevin

Private Declare Function apiGetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nsize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = ""
End If
End Function

Sub GetUserNameTest()
MsgBox fOSUserName
End Sub

Regards

Trevor
 
*If* they have their name in the User Details dection of Tools, Options -
then
Application.UserName

For network login details, you'll need an API call. There is one for NT
(Can't find it right now - don't use NT), not so sure about others.

HTH
Roger
Shaftesbury (UK)
 
Kevin

I use the following function to achieve the same as you
are looking for....

I then have an "auto_open" macro which acts differently
depending on the username which is returned

I got this from another user on the newsgroup


HTH

Michael Bond


Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize
As Long) As Long


Private Function ReturnUserName() As String
' returns the Domain User Name
Dim rString As String * 255, sLen As Long, tString As
String
tString = ""
On Error Resume Next
sLen = GetUserName(rString, 255)
sLen = InStr(1, rString, Chr(0))
If sLen > 0 Then
tString = Left(rString, sLen - 1)
Else
tString = rString
End If
On Error GoTo 0
ReturnUserName = (Trim(tString))
End Function
 
Back
Top