I'm using Excel 97/Win NT 4.0.
I want to get the UserName from the system, whenever a user loads the
Workbook. The UserName is known to the system, because whenever the user
logs on, he must supply the name.
The programs needs this identification for seting the user's authorization.
What lines of code will do the job?
Thanks
Kobi Idove
or GetUserName API
- Jens -
ELOP <Jacob...@hotmail.com> schrieb in im Newsbeitrag:
uWxYdqDW$GA....@cppssbbsa02.microsoft.com...
Application.UserName will return the name of the user registered to use
Excel. If that won't suffice, Jake Marx recently posted an API method
... see below.
HTH,
Ron :-)
From - Tue Dec 28 09:48:27 1999
From: "Jake Marx" <Jake...@americawest.com>
Subject: Re: networklogon name upon tracking changes
Date: Mon, 27 Dec 1999 09:32:18 -0700
Newsgroups: microsoft.public.excel.programming
Xref: cppssbbsa01.microsoft.com
microsoft.public.excel.programming:102350
Jake Marx wrote:
To get the username from the operating system, you can use the
GetUserName()
function from the Windows API. Here's an example (just paste this code
into
a standard module to use it):
Public Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Public Function sUserName() As String
Dim sName As String * 256
Dim nNullPos As Integer
On Error GoTo ErrHandler
If GetUserName(sName, 256) Then
nNullPos = InStr(sName, vbNullChar)
If nNullPos Then
sUserName = Left$(sName, nNullPos - 1)
Else
sUserName = sName
End If
End If
ExitRoutine:
Exit Function
ErrHandler:
Resume ExitRoutine
End Function
So, calling sUserName() in your code will return a string representing
the
OS username. If an error is encountered, it will return an empty
string.
Another option would be to use VBA's Environ function to get the
username.
Try typing ?Environ("username") into the Immediate window to see if that
gives you what you want. I think this should work for all Windows
operating
systems, but I don't know for sure.
Regards,
Jake Marx
You see, using Jake's solution seems to work at first, but there will be
some "|" for each empty fields at the end of the username. ex:
"Robertb1|||||||||||||||||". This is due to the C coding in the
"advapi32.dll". You must fin a way to convert the C string to a VB string..
you will then be able to "trim$" it if needed.
here's something that was given to me that helped understand it all:
---------------------------------------------
Jose's VB Tips & Tricks
Obtain the User Network Logon Name
Obtaining the network logon name is a commonly needed utility function for
many applications and can be easily accomplished with a single call to the
Win32 API suite of networking functions.
The WNetGetUser function retrieves the current default user name or the user
name used to establish a network connection. This is among the simplest of
API calls.
Since this function has both ANSI and Unicode versions, the function is
aliased. As a matter of habit, I always prefix the Win32 API declarations
with "w32_" so that I can use the actual function or sub name as my VB
procedure name. Remember that API declarations are now case-sensitive.
Here's the declaration:
Private Declare Function w32_WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA"
( _
ByVal lpszLocalName As String, _
ByVal lpszUserName As String, _
lpcchBuffer As Long) As Long
This function takes three parameters: lpName, lpUserName, lpnLength.
lpszLocalName
Points to a null-terminated string that specifies either the name of the
local device to return the user name for or the name of a network that the
user has made a connection to.
If this parameter is NULL, Windows returns the name of the current user for
the process.
If this parameter is a network name and the user is connected to that
resource using different names, the network provider may not be able to
resolve which user name to return. In this case, the provider may make an
arbitrary choice from the possible user names.
lpszUserName
Points to a buffer that receives the null-terminated user name.
lpcchBuffer
Points to a variable that specifies the size, in characters, of the buffer
pointed to by lpszUserName. If the call fails because the buffer is not big
enough, this variable contains the required buffer size.
We're also going to need a single constant added to the declarations
section:
Private Const NO_ERROR = 0
This constant is used to test the return value of the API call.
Since lpszUserName is an output parameter of the WNetGetUser function, we'll
add a simple utility function for converting null a terminated C string to a
VB string. Here's that code:
Public Function CStringToVBString(psCString As String) As String
' **********
' Purpose: Convert a C string to a VB string
' Parameters: (Input Only)
' psCString - the C string to convert
' Returns: The converted VB string
' Notes:
' Returns everything to the left of the first Null character
' **********
Dim sReturn As String
Dim iNullCharPos As Integer
iNullCharPos = InStr(psCString, vbNullChar)
If iNullCharPos > 0 Then
' return everything left of the null
sReturn = Left(psCString, iNullCharPos - 1)
Else
' no null, return the original string
sReturn = psCString
End If
CStringToVBString = sReturn
End Function
Finally, here's the wrapper around Win32's WNetGetUser function:
Public Function WNetGetUser() As String
' **********
' Purpose: Retrieve the network user name
' Paramters: None
' Returns: The indicated name
' Notes:
' A zero-length string is returned if the function fails
' **********
Dim lpUserName As String
Dim lpnLength As Long
Dim lResult As Long
lpnLength = 256
lpUserName = Space(lpnLength)
lResult = w32_WNetGetUser(vbNullString, lpUserName, lpnLength)
If lResult = NO_ERROR Then
WNetGetUser = CStringToVBString(lpUserName)
Else
WNetGetUser = ""
End If
End Function
Rather than return a status code or Boolean value, the function simply
returns the value from the API call or a zero length string. Unlike some API
procedures, the probability of failure in this procedure is minimal so the
extra test would only serve to add work to any calling routine. If a
possibility of failure is a vital concern, a more robust wrapper function
could be constructed.
For simplicity, I've ignored the lpszLocalName parameter in the original API
call and simply passed a null string. However, it would be easy enough to
restore that parameter to the wrapper function.
-----------------------------------------------------
Hope this helps!
Alex Dandurand
MIT Trainer
Costco Canada
alex.da...@costco.ca
Here is the code that I use:
Public Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0 'The Function call was successful
Public Function FindNetUserName() As String
' Buffer size for the return string.
Const lpnLength As Integer = 255
' Get return buffer space.
Dim status As Integer
' For getting user information.
Dim lpName, lpUserName As String
' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)
' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)
' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else
' An error occurred.
lpUserName = "Unknown"
End
End If
FindNetUserName = lpUserName
End Function
But it's not really necessary to use that technique because the API call
itself tells you how long the returned string is:
Public Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Sub a()
Debug.Print sUserName
End Sub
Public Function sUserName() As String
Dim sName As String * 256
Dim CharCount As Long
CharCount = 256
If GetUserName(sName, CharCount) Then _
sUserName = Left$(sName, CharCount - 1)
End Function
GetUserName modifies the value in CharCount to the length of the returned
string plus one for the ASCII 0.
--
Jim Rech
Excel MVP
ELOP wrote in message ...
'***********************************************************
'** Reads the WINDOWS Login Identity **
'***********************************************************
Public Function ReturnUserName() As String
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 = UCase(Trim(tString))
End Function
--
Mark mailto:ts...@callnetuk.com
Cathy mailto:cj.tow...@lineone.net
URL http://www.callnetuk.com/home/tssd/default.htm
news:uk.education.openuniversity
"ELOP" <Jacob...@hotmail.com> wrote in message
news:u125JXmW$GA.294@cppssbbsa05...