Finding current user ID

B

Bo Hansson

Have a Word VBA application running on a computer with several user
accounts. How can I find out the ID of the current user?

/BosseH
 
K

Karl E. Peterson

Bo said:
Have a Word VBA application running on a computer with several user
accounts. How can I find out the ID of the current user?

Do you mean username? If so, ...

Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Const UNLEN As Long = 256 ' Maximum username length

Public Function CurrentUserName() As String
Dim Buffer As String
Dim nLen As Long
Const NameLength = UNLEN + 1
' Call ANSI version for non-NT systems.
Buffer = Space$(NameLength)
nLen = Len(Buffer)
If GetUserName(Buffer, nLen) Then
CurrentUserName = Left$(Buffer, nLen - 1)
End If
End Function
 
K

Karl E. Peterson

Bo said:
Have a Word VBA application running on a computer with several user
accounts. How can I find out the ID of the current user?

Then again, there's also Application.UserName if you're after what Office,
rather than the system, consider it to be.
 
B

Bo Hansson

Thanks!

But when I paste your code in my application for test, the declaration turns
red-coloured immediately. Something wrong with that ?

BosseH
 
K

Karl E. Peterson

Bo said:
Thanks!

But when I paste your code in my application for test, the
declaration turns red-coloured immediately. Something wrong with that
?

Did you correct the linewrap? (I nearly always indent entire code samples,
so that wrapped lines will be immediately obvious.)
 
T

Tony Jollans

What you probably want is ...

Environ("UserName") - this is the Windows logon userid.

Application,Username is the Office username which is easily changed by users
and not really meaningful.
 
B

Bo Hansson

Thanks - it works GREAT!

/BosseH
Karl E. Peterson said:
Did you correct the linewrap? (I nearly always indent entire code
samples,
so that wrapped lines will be immediately obvious.)
 
K

Karl E. Peterson

Tony said:
What you probably want is ...

Environ("UserName") - this is the Windows logon userid.

Application,Username is the Office username which is easily changed
by users and not really meaningful.

Likewise, environment variables are far less reliable than the API. Use
GetCurrentUser if you want to be sure.
 
T

Tony Jollans

I don't doubt you for a second, but how does one change the username
environment variable? I thought it always picked up the same as
GetCurrentUser - what is the point of it otherwise?
 
K

Karl E. Peterson

Tony said:
I don't doubt you for a second, but how does one change the username
environment variable?

======================================
C:\>set username
USERNAME=kep

C:\>set username=NoneOfYourBusiness!

C:\>set username
USERNAME=NoneOfYourBusiness!
======================================

Of course, that only affects the current process.
I thought it always picked up the same as
GetCurrentUser - what is the point of it otherwise?

Like many conveniences, that's all it is.

I guess your question is similar to, "if you can die in a car with airbags,
what's the point of them anyway?" Maybe it's good, maybe not. Depends on
the crash.

In this case, you *can* take the uncertainty out of it, though. Or at least
lots more of it. (You familiar with OS support for impersonating users?
<g>)
 

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