Finding User Id

C

CrankyLemming

Hi

Some time ago I stumbled across some code in this group, that looks at
wherever the computer stores the current user info and reports back
with the user's ID. Trouble is, I've searched under every variation I
can think of, but cannot find it.

Does anyone know what I'm babbling about, and if so, can they point me
in the right direction.

Any help would be most appreciated.

Steve
 
H

Hari

Hi Gord,

Thanx for that url.

I have a slightly different need.

I would like to get the name of the person rather than the Lan ID with which
the person logs on to Win 2000.

That is suppose I have a user whose name is "Bam Kile" and whose Lan ID
while logging on to Windows is bkile then I am not intersted in the Lan id
bkile but
in the Name "Bim Kile" .I tried all the codes in ur url and some had the
advantage of getting the domain , computer name but not what I wanted.

Is it possible to get the actual name of the person?.

Regards,
Hari
India


Gord Dibben said:
Steve

Several variations of code to return username at this google search result.

http://snipurl.com/7qgi

Note: these return the logon name

Gord Dibben Excel MVP
 
G

Gord Dibben

Hari

How about the Excel user name?

Sub user()
MsgBox Application.username
End Sub

Gord

Hi Gord,

Thanx for that url.

I have a slightly different need.

I would like to get the name of the person rather than the Lan ID with which
the person logs on to Win 2000.

That is suppose I have a user whose name is "Bam Kile" and whose Lan ID
while logging on to Windows is bkile then I am not intersted in the Lan id
bkile but
in the Name "Bim Kile" .I tried all the codes in ur url and some had the
advantage of getting the domain , computer name but not what I wanted.

Is it possible to get the actual name of the person?.

Regards,
Hari
India
 
H

Hari

Hi Gord,

I believe the Excel user name is picked up from Tools-Options-General which
could be easily tampered with. ( I tried it just now)

Why I wanted this feature is Im distributing some files to different
geographic locations which lots of users will work on. Im running a
workbook_beforesave macro
in that and the file would save only when the data is consistent. I thought
if the data is internally consistent then somewhere within one of the
worksheets I would like to automatically record the name of the person while
the workbook is getting saved ( from his Win 2000 Log in Name corresponding
to the Lan ID) so that in future if any problem is there ( due to external
inconsistency) I could ask the person directly. Having Lan ID will not be
useful to me because there are ... well over 40,000 people in my
organization so lan ID route is not the right one for my situation.
Presently I have to record the name of the person once s/he sends a mail to
me and there are lots of such files I get, hence I thought if there is some
macro which could get what I want ......

I do basic stuff with Excel macros like If statement etc but dont know about
this...

Regards,
Hari
India


Gord Dibben said:
Hari

How about the Excel user name?

Sub user()
MsgBox Application.username
End Sub

Gord
 
G

Gord Dibben

Hari

I think we're both going to have to wait for someone else to jump in with a
suggestion.

I'm not much help beyond the basics.

Gord
 
F

Frank Kabel

Hi Hari
where do you store this name (e.g. what kind of network do you use). If
I look at my workdeskt my real name is only stored in a separate file
(used by our administrators) but not within Windows (only a short
networkname is used).

So where can you find this user name manually in your environment
 
I

icestationzbra

this post is dave peterson's (intellectual property)...

see if this is what you require.

*****

'saving network username into txt file

Option Explicit
Private Sub Workbook_Open()

Dim LogDir As String
Dim LogFile As String
Dim myFileNum As Long
Dim testDir As String

LogDir = "\\servername\sharename\foldername"
LogDir = "C:\my documents\excel"
LogFile = LogDir & "\log.txt"

testDir = ""
On Error Resume Next
testDir = Dir(LogDir, vbDirectory)
On Error GoTo 0

If testDir = "" Then
'not connected or spelling error!
Exit Sub
End If

myFileNum = FreeFile()
Open LogFile For Append As #myFileNum
Print #myFileNum, ThisWorkbook.FullName & vbTab _
& Application.UserName & vbTab & fOSUserName & Now
Close #myFileNum

End Sub

'Then right click on the project (hit ctrl-r to see it if its no
shown). 'Select
'Insert, then Module.

'Paste this in:

Option Explicit
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

'=====
'When you want to view the log, just open excel and d
'File|open--choose
'delimited by tab when you see the import wizard
 
C

CrankyLemming

Gord said:
Several variations of code to return username at this google search result.

http://snipurl.com/7qgi

Note: these return the logon name

Logon name. Don't know why I had no luck. Anyhow, I got the follwoing
code through following your link (thanks, by the way):

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

Sub GetUserNameTest()
MsgBox fOSUserName
End Sub
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

Trouble is, I can't see how to show the username in a cell, say
Sheet1, A1.

Any further ideas?

teve
 
H

Hari

Hi Icestationzbra,

Even this picks up the name from the tools-> option -> general.

At first it displayed the name as desired but when I changed the name in the
Tools->Option->General to some gibberish "hyuifr" then the Log file also
showed it as
"hyuifr"

Regards,
Hari
India
 
H

Hari

Hi Frank,

Im sorry , dunno anything about "what kind of network do you use" . A novice
about techy stuff. I understand that by network u were not asking my
operating system name as I had already mentioned that in the initial mail as
Win 2000 Pro.

Anyway as per "my real name is only stored in a separate file (used by our
administrators) but not within Windows" I understand that it might not be
possible for me to achieve what I wanted.

Regards,
Hari
India
 
F

Frank Kabel

Hi
if this name is only stored in this separate file you'll need at least
this file in an accessible location (probably not the case). So it is
not possible 8how should your computer KNOW your real name?)
 
D

Dave Peterson

Actually, it uses the logon name and the application.username:
Print #myFileNum, ThisWorkbook.FullName & vbTab _
& Application.UserName & vbTab & fOSUserName & Now

(Note the fOSUserName)

And just from a personal standpoint, I think I'd use that logon name. There may
be a few people with the same name--but each has his/her own unique id for
logging on (and if multiple people use a common id, you could still get it back
to that assigned id).
 
H

Hari

Hi Dave,

Thanx a lot for getting involved with the thread.

In my situation, Im giving distributing my file to around 20 groups who are
from different geographical regions. In the file I already have a cell where
the name of the geographical region is being recorded. So, if 2 names are
same then it will be no problem for me since seeing the name and the geo
region, I will know which person it refers to.

For me the problem is that if I record the Lan ID it will be very difficult
to "orally decode" which name this corresponds to ( inspite of having the
help of Geo. region). I dont have access to the database where lan id and
real names mapping is stored ( Frank Kabel said that this data is stored in
a separate file) so I think it might not be possible for me to get what I
wanted.

Regards,
Hari
India
 
D

Dave Peterson

I don't think I'm gonna be much help.

I don't have access to a network and this gave me the same name as the
fOSUsername API function did.

Option Explicit
Sub testme()

Dim WSH As Object
'Dim WSH As IWshRuntimeLibrary.WshNetwork

Set WSH = CreateObject("WScript.network")
'Set WSH = New IWshRuntimeLibrary.WshNetwork

MsgBox WSH.ComputerName
MsgBox WSH.UserName

End Sub


After you test this and it doesn't work, maybe it would be a good question to
ask in one of the .scripting newsgroups.

If you do, explain that you want to use it in excel (or post back here and I'm
sure others will help make it work in excel).

If you don't get a sufficient answer from there, maybe there's some registry
setting that your company uses that can be inspected. You may want to fire a
question toward your IT department, too.

And a last possible workaround. If the list is small, build your own table of
id's vs. human names.

Then use some kind of =vlookup() to do your translation.


Hi Dave,

Thanx a lot for getting involved with the thread.

In my situation, Im giving distributing my file to around 20 groups who are
from different geographical regions. In the file I already have a cell where
the name of the geographical region is being recorded. So, if 2 names are
same then it will be no problem for me since seeing the name and the geo
region, I will know which person it refers to.

For me the problem is that if I record the Lan ID it will be very difficult
to "orally decode" which name this corresponds to ( inspite of having the
help of Geo. region). I dont have access to the database where lan id and
real names mapping is stored ( Frank Kabel said that this data is stored in
a separate file) so I think it might not be possible for me to get what I
wanted.

Regards,
Hari
India
 
D

Dave Peterson

Some minor testing at work (xl2k and win2k) showed that wsh.username
was the same as the logon name.

But if you're using Outlook as your email client (and you don't have a
problem with security settings in Outlook), maybe something like:

Option Explicit
Sub testme()

Dim myErrNum As Long
Dim myOL As Object
Dim myNS As Object

' Dim myOL As Outlook.Application
' Dim myNS As Outlook.NameSpace
' Set myOL = New Outlook.Application

On Error Resume Next
Set myOL = GetObject(, "Outlook.Application")
myErrNum = Err.Number
On Error GoTo 0

If myErrNum = 429 Then
Set myOL = CreateObject("Outlook.application")
End If

Set myNS = myOL.GetNamespace("MAPI")

MsgBox myNS.CurrentUser

End Sub

You can find a bunch of info about automating Outlook at:
http://www.dicks-clicks.com/excel/olAutomating.htm




Dave Peterson said:
I don't think I'm gonna be much help.

I don't have access to a network and this gave me the same name as the
fOSUsername API function did.

Option Explicit
Sub testme()

Dim WSH As Object
'Dim WSH As IWshRuntimeLibrary.WshNetwork

Set WSH = CreateObject("WScript.network")
'Set WSH = New IWshRuntimeLibrary.WshNetwork

MsgBox WSH.ComputerName
MsgBox WSH.UserName

End Sub


After you test this and it doesn't work, maybe it would be a good question to
ask in one of the .scripting newsgroups.

If you do, explain that you want to use it in excel (or post back here and I'm
sure others will help make it work in excel).

If you don't get a sufficient answer from there, maybe there's some registry
setting that your company uses that can be inspected. You may want to fire a
question toward your IT department, too.

And a last possible workaround. If the list is small, build your own table of
id's vs. human names.

Then use some kind of =vlookup() to do your translation.
 
Top