Results 1 to 9 of 9

Thread: [RESOLVED] Login User - Full Name

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved [RESOLVED] Login User - Full Name

    I currently use the following code to get the User name of the logged in user.

    Code:
    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
    (ByVal lpBuffer As String, nSize As Long) As Long
    Public Function CurrentUser() As String
    '* Function to get the current logged on user in windows *
    Dim strBuff As String * 255
    Dim x As Long
        CurrentUser = ""
        x = GetUserName(strBuff, Len(strBuff) - 1)
        If x > 0 Then
            'Look for Null Character, usually included
            x = InStr(strBuff, vbNullChar)
            'Trim off buffered spaces too
            If x > 0 Then
                CurrentUser = Left$(strBuff, x - 1)    'UCase is optional
            Else
                CurrentUser = Left$(strBuff, x)
            End If
        End If
    End Function
    But what I want to do is get the long name.

    Thus if the user login name is smitty I could retrieve the long name of Yolanda Smitt

    I have already checked out RobDog's How do I get the current windows user name?
    Signature Under Construction

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Login User - Full Name

    You are unlikely to have accurate information that you could query on the Users PC of the Full name, and if you have any PC's that can be used by multiple users even less likely.

    You do have a few options, if you use Outlook at your company (i am assuming it's a company) you could query Active Directory using the Login name and return the Users full name that way. If you use GroupWise or Novell you could query EDirectory, and i would assume other Email & Networking system's would have there own User Directory system you could Query. This would be the best way.

    Could you tell us what email system / Network you have in Place ?


    Other than that you must have some systems at you company that store both Login / Username and Full name information. If you can find out which one's, You could query their databases.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Login User - Full Name

    try application.username, but it is only valid if it was correct at time of install
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Login User - Full Name

    Hi Guys,

    NeedSomeAnswers,
    We use Outlook.

    I thought that there was a long name associated with the user login name.

    Can you advise as to how I would go about querying the Address books in Outlook.
    westconn1,
    application.username only gives the login username, i.e. smitty
    Signature Under Construction

  5. #5
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Login User - Full Name

    Here is a link to some code for Querying Active Directory

    You will need to add references to ADO & Active DS Type Library

    http://www.freevbcode.com/ShowCode.Asp?ID=710

    But it should work depending on Your active Directory Security.

    Just call the function with your username.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Login User - Full Name

    application.username only gives the login username, i.e. smitty
    on mine, it returns my full name as that was inserted when office was installed
    there is no guarantee that the users full name is installed on the computer anywhere, office apps are the most likely place,
    i can find my full name from my email account or
    the registered owner, registry key from windowsNT, current version,
    but again it depends if those were filled in during installation
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Login User - Full Name

    In a networked company environment, it is unlikely that you will be able to get Full username information from a users PC.

    Often Support departments use Imaging software for installing O/S & other software because it is so much quicker, and will use the companies group/site licence or whatever it is called.

    Even if they do not use images, PC's are not often registered in a users name due the fact that they have licences to cover a group of people rather than individual licences.

    In the case of email accounts, in the Enterprise Outlook get's it's information from AD, Groupwise from EDirectory e.t.c and it is easier to query these LDAP databases to get your user full name.

    Plus if you use this approach your Code will be PC independent and will work regardless of which company PC the user logs into.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Login User - Full Name

    In order to retrieve the full name for given network user id, we can call the NetUserGetInfo API function.

    Google this function you can find 7000+ entries. It is also included in AllAPI tool.

    This function is very hard to use in VB6 as it required many other support functions.

    One topic from Microsoft: http://support.microsoft.com/kb/151774

    There is a long coding but it's very easy to use from Dev Ashish:
    Get full name of the user currently logged in to the system.
    I think this is the best code for this topic.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Login User - Full Name

    Many thanks guys for all your suggestions.

    In the end I went with the following modified from the original onRobDog's Outlook Global Address List link in his Sig.

    Code:
    Option Explicit
    ' Email Global Address List
    'Add reference to MS CDO 1.21 Library
    'Add reference to MS Office Outlook xx.x Object Library
    Private moApp As Outlook.Application
    Private moNS As Outlook.Namespace
    Private moCDO As MAPI.Session
    Private Const cdoPR_DISPLAY_NAME As Long = &H3001001E
    Private Const cdoPR_ACCOUNT As Long = &H3A00001E
    Private Const cdoPR_GIVEN_NAME As Long = &H3A06001E
    Private Const cdoPR_SURNAME As Long = &H3A11001E
    Private Const cdoPR_EMAIL As Long = &H39FE001E
    
    Public Sub GetCurrentName(ByRef Login As String, ByRef Forename As String, _
                                ByRef Surname As String, ByRef EmailAddress As String, _
                                ByRef DisplayName As String)
    On Error GoTo No_Bugs
    Dim fn As String
    Dim sn As String
    Dim ln As String
    Dim vDetails As Variant
    Dim lRet As Long
    Dim oAEntries As Object
    Dim oAEntry As Object
    Dim i As Long
    Dim ii As Long
    
    'INITIALIZE OOM (JUST SO CDO CAN LOGON TO INSTANCE)
        Set moApp = GetObject(, "Outlook.Application")
        If TypeName(moApp) = "Nothing" Then
            Set moApp = New Outlook.Application
        End If
        Set moNS = moApp.GetNamespace("MAPI")
        'INITIALIZE CDO
        Set moCDO = CreateObject("MAPI.Session")
        moCDO.Logon "", "", False, False
    
        Login = ""
        Forename = ""
        Surname = ""
        Set oAEntries = moCDO.AddressLists.Item("Global Address List").AddressEntries
        Set oAEntry = oAEntries.Session.CurrentUser
        For ii = 1 To oAEntry.Fields.Count
            vDetails = oAEntry.Fields(ii).Value
            On Error Resume Next
            lRet = UBound(vDetails)
            If Err.Number <> 0 Then
                Select Case oAEntry.Fields(ii).ID
                Case cdoPR_SURNAME
                    Surname = vDetails
                Case cdoPR_GIVEN_NAME
                    Forename = vDetails
                Case cdoPR_ACCOUNT
                    Login = vDetails
                Case cdoPR_DISPLAY_NAME
                    DisplayName = vDetails
                Case cdoPR_EMAIL
                    EmailAddress = vDetails
                End Select
            End If
        Next ii
        
        Set oAEntries = Nothing
        Set oAEntry = Nothing
        moCDO.Logoff
        Set moCDO = Nothing
        Set moNS = Nothing
        Set moApp = Nothing
        
        Exit Sub
    No_Bugs:
        If Err.Number = 429 Then
            Resume Next
        Else
            MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
        End If
    End Sub
    Signature Under Construction

Posting Permissions

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



Click Here to Expand Forum to Full Width