Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Basalt, Colorado, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Obtain window 2000 userid (not username) (XP)

    Hi all,
    I would like to retrieve the windows userid from the windows 2000 environment. The vba username() function returns the full name, not the userid.
    I would like to use it to point a macro to the proper "My Documents" folder, based on who is logged on.

    Thanks,
    Dan

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Obtain window 2000 userid (not username) (XP)

    Try something like:
    <pre>sUserName = environ("username")</pre>


    Steve

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Obtain window 2000 userid (not username) (XP)

    I believe I owe these respectively to <!profile=Kevin>Kevin<!/profile> and Jan Karel <!profile=Pieterse>Pieterse<!/profile>, but any errors are all mine.

    First, a short cut way if you are in a corporate environment where the username is set in the environment space:

    Public Function GetLoginUserName() As String
    GetLoginUserName = vbNullString
    On Error Resume Next
    GetLoginUserName = Environ("username")
    End Function

    ... and the appropriate Windows API function:

    Private Const MAX_USERNAME As Long = 256

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

    Function rgbGetUserName() As String
    'return the name of the user
    Dim strTmp As String
    Dim intPos As Integer
    strTmp = Space$(MAX_USERNAME)
    If GetUserName(strTmp, Len(strTmp)) Then _
    intPos = InStr(strTmp, Chr$(0))
    If intPos Then
    rgbGetUserName = Left$(strTmp, intPos - 1)
    Else
    rgbGetUserName = strTmp
    End If
    End Function
    -John ... I float in liquid gardens
    UTC -7ąDS

  4. #4
    Lounger
    Join Date
    Jan 2001
    Location
    Basalt, Colorado, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obtain window 2000 userid (not username) (XP)

    Steve and John,
    Thanks!
    I was totally unaware of the 'environ()' function. It, and the windows API function work great.

    Dan A.

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Obtain window 2000 userid (not username) (XP)

    Another pssibility for good measure using Windows Scripting Host <pre>
    Function wshGetUserName()
    Dim wshNet
    Set wshNet = CreateObject("WScript.Network")
    wshGetUserName = wshNet.UserName
    Set wshNet = Nothing
    End Function</pre>


    Does Windows not take care of assigning the correct user profile ?

    Andrew C

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Flims, Switzerland
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obtain window 2000 userid (not username) (XP)

    (Edited by HansV to make URL clickable - see <!help=19>Help 19<!/help>)

    You should not be deriving the path of the My Documents folder using the username. There are other methods which return the My Documents path, e.g. http://www.thescarms.com/vbasic/specialfolders.asp - see the CSIDL_PERSONAL folder.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •