Thanks Thanks:  0
Likes Likes:  0
Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Get Windows user name

  1. #11
    Board Regular
    Join Date
    Sep 2003
    Posts
    90
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Get Windows user name

    how do I invoke this last function? does it need an agrument?

    GetUserName(strLDAP)

  2. #12
    Board Regular
    Join Date
    Sep 2003
    Posts
    90
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Get Windows user name

    Never mind I used a different function

  3. #13
    New Member
    Join Date
    Jun 2011
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Get Windows user name

    Hi GriZlore,
    As above, how do you envoke the function so it displays the full name in a cell?

    Thanks

  4. #14
    Board Regular
    Join Date
    May 2009
    Location
    Perth, Australia
    Posts
    1,638
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Get Windows user name

    Hi,

    Try modificated Code

    Code:
    Sub Cell()
    'originally coded as VB script by A.Vials, converted to VBA by Sly
    Dim objInfo
    Dim strLDAP
    Dim strFullName
      
    Set objInfo = CreateObject("ADSystemInfo")
    strLDAP = objInfo.UserName
    Set objInfo = Nothing
    strFullName = GetUserName(strLDAP)
      
    Range("A1") = strFullName '<== Adjust cell Reference
      
    End Sub
     
    Function GetUserName(strLDAP)
      Dim objUser
      Dim strName
      Dim arrLDAP
      Dim intIdx
      
      On Error Resume Next
      strName = ""
      Set objUser = GetObject("LDAP://" & strLDAP)
      If Err.Number = 0 Then
        strName = objUser.Get("givenName") & Chr(32) & objUser.Get("sn")
      End If
      If Err.Number <> 0 Then
        arrLDAP = Split(strLDAP, ",")
        For intIdx = 0 To UBound(arrLDAP)
          If UCase(Left(arrLDAP(intIdx), 3)) = "CN=" Then
            strName = Trim(Mid(arrLDAP(intIdx), 4))
          End If
        Next
      End If
      Set objUser = Nothing
      
      GetUserName = strName
      
    End Function
    Biz

  5. #15
    New Member
    Join Date
    Feb 2013
    Location
    West Chester, Pennsylvania
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Get Windows user name

    Quote Originally Posted by lenze View Post
    The code to return the log-on name is
    Code:
    Environ("UserName")
    Code:
    Application.UserName
    returns the application name set under Tools>Options

    lenze


    This is just what I was looking for. I just did a backflip at my desk

  6. #16
    New Member vicsar's Avatar
    Join Date
    May 2013
    Location
    Heredia, Costa Rica
    Posts
    22
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Re: Get Windows user name

    Here is my take on this, a practical example, by no means refined but it gets the job done.

    Code:
    Sub init()
    ' Used to store the domain name and username information
        Dim strUserDomainAndName As String
    
        strUserDomainAndName = Environ("UserDomain") & "\" & Environ("UserName")
    
    ' Begin inputing date information
        Range("A3").Select
    ' Go to the last row
        Selection.End(xlDown).Select
        Selection.Offset(1, 0).Select
    ' Insert formula
        ActiveCell.FormulaR1C1 = strUserDomainAndName
    
    ' Begin inputing date information
        Range("B3").Select
    ' Go to the last row
        Selection.End(xlDown).Select
        Selection.Offset(1, 0).Select
    ' Insert formula
        ActiveCell.FormulaR1C1 = "=NOW()"
    ' Copy and paste values
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    ' Disable marching ants around copied range
        Application.CutCopyMode = False
    
    ' Offset to the right
        Selection.Offset(0, 1).Select
    End Sub

  7. #17
    New Member
    Join Date
    Nov 2014
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Get Windows user name

    Quote Originally Posted by vicsar View Post
    strUserDomainAndName = Environ("UserDomain") & "" & Environ("UserName")
    Awesome exactly what I was looking for!

  8. #18
    New Member
    Join Date
    Oct 2011
    Posts
    32
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Get Windows user name

    Hello,
    If I want to display a username in a message box how do I add that to VBA?

    I am currently using this to get the message box to pop up if a cell needs a value:

    Private Sub Worksheet_Deactivate()
    Dim rngCheck As Range
    Dim cel As Range
    Dim j As String
    Dim i As Integer
    Dim Ws As Worksheet

    Set Ws = Sheets("RATE ")
    Set rngCheck = Ws.Range("B8:B11")

    i = 0
    For Each cel In rngCheck
    If IsEmpty(cel) Then
    i = i + 1
    j = j & cel.Address & vbNewLine
    End If
    Next cel

    If i = 0 Then Exit Sub

    Ws.Activate
    MsgBox "Sorry, you must enter a value!"
    End Sub

    **Goal is to have it say "Sorry, USERNAME you must enter a value!"

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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