Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Finding current user ID

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bo Hansson - 23 Nov 2005 17:21 GMT
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
Karl E. Peterson - 23 Nov 2005 17:35 GMT
> 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

--
Working without a .NET?
http://classicvb.org/
Bo Hansson - 23 Nov 2005 18:00 GMT
Thanks!

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

BosseH
>> Have a Word VBA application running on a computer with several user
>> accounts. How can I find out the ID of the current user?
[quoted text clipped - 20 lines]
> Working without a .NET?
> http://classicvb.org/
Karl E. Peterson - 23 Nov 2005 18:02 GMT
> 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.)
--
Working without a .NET?
http://classicvb.org/

> BosseH
>>> Have a Word VBA application running on a computer with several user
[quoted text clipped - 21 lines]
>> Working without a .NET?
>> http://classicvb.org/
Bo Hansson - 23 Nov 2005 18:16 GMT
Thanks - it works GREAT!

/BosseH
>> Thanks!
>>
[quoted text clipped - 34 lines]
>>> Working without a .NET?
>>> http://classicvb.org/
Karl E. Peterson - 23 Nov 2005 17:37 GMT
> 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.
--
Working without a .NET?
http://classicvb.org/
Tony Jollans - 23 Nov 2005 18:08 GMT
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.

--
Enjoy,
Tony

> > Have a Word VBA application running on a computer with several user
> > accounts. How can I find out the ID of the current user?
[quoted text clipped - 4 lines]
> Working without a .NET?
> http://classicvb.org/
Karl E. Peterson - 23 Nov 2005 19:52 GMT
> 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.
--
Working without a .NET?
http://classicvb.org/
Tony Jollans - 23 Nov 2005 20:17 GMT
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?

--
Enjoy,
Tony

> > What you probably want is ...
> >
[quoted text clipped - 8 lines]
> Working without a .NET?
> http://classicvb.org/
Karl E. Peterson - 23 Nov 2005 20:40 GMT
>>> Environ("UserName") - this is the Windows logon userid.
>>>
[quoted text clipped - 6 lines]
> 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>)
--
Working without a .NET?
http://classicvb.org/
Tony Jollans - 23 Nov 2005 21:40 GMT
Thanks Karl!

I guess it's possible to fool any program if you try hard enough :-)

--
Enjoy,
Tony

> >>> Environ("UserName") - this is the Windows logon userid.
> >>>
[quoted text clipped - 34 lines]
> Working without a .NET?
> http://classicvb.org/
Karl E. Peterson - 23 Nov 2005 22:40 GMT
> Thanks Karl!
>
> I guess it's possible to fool any program if you try hard enough :-)

Keep that in mind, and you'll go far.  ;-)
--
Working without a .NET?
http://classicvb.org/

>>>>> Environ("UserName") - this is the Windows logon userid.
>>>>>
[quoted text clipped - 34 lines]
>> Working without a .NET?
>> http://classicvb.org/

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.