Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LAN User ID's accessing Excel Spreadsheet 2

Status
Not open for further replies.

protector

Technical User
Mar 16, 2002
18
NZ
Hello

I recently created a spread sheet to provide reporting on regular checks that need to be carried out. Multiple users access this spread sheet and I would like to be able to store their user id's in a cell when they open the spread sheet. As only one user can access this spread sheet at a time, I see that Excel has the logged in user in the error message. Can I access this data and have it entered in a cell.

Any help appreciated
Craig
 
unsure if this is what you need but...

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

'KPD-Team 1998
'URL: [URL unfurl="true"]http://www.allapi.net/[/URL]
'E-Mail: KPDTeam@Allapi.net
Dim strUserName As String

'Create a buffer
strUserName = String(100, Chr$(0))
'Get the username
GetUserName strUserName, 100
'strip the rest of the buffer
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)

'Show the temppath and the username
MsgBox "Hello " + strUserName
 
Surely you can retrieve the user ID in VB using Environ$("User"). I wouldn't have thought you would have to use API calls.

Ed Metcalfe.
 
Using the API is going to be the most reliable way to retrieve the user's Login.

On NT/2K systems, you can use Environ$("UserName"), but UserName isnt present on 9X machines.

You may find it simpler to just use the Username property of Excel's Application object. However, on my 2K machine this property returns my network user Login, jsh999, while on my 98 machine, it returns my user name, Jon Hawkins. Jon Hawkins
 
Hi Guys

Thanks for all the help. I did forget to mention that this is on an NT network, so could you tell me how to use the Environ$("User") code as this is new to me.

Thanks
Craig
 
You could also use :

strUser = CreateObject("Wscript.Network").UserName

if you have WSH (Windows Scripting Host) active on your system.

I have found that Environ$("User") does not always work, even on NT systems.

A.C.
 
Hi Everybody

Thanks for the code samples. I trialled all the code supplied and ended up using the code below. The help is really appreciated.

Private Sub getname_Click()
Dim userName As String

userName = CreateObject("Wscript.Network").userName

MsgBox userName

End Sub

Craig
[2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top