VBA TIPS
Retrieving the current default user name

The WNetGetUser function retrieves the current default user name, or the user name used to establish a network connection.

See details

Place following in the standard module.

Option Explicit

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
                         (ByVal lpName As String, _
                          ByVal lpUserName As String, _
                          lpnLength As LongAs Long

Private Const NO_ERROR = 0
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&

Sub Getusername()
    Dim strBuf As String, lngUser As Long, strUn As String
    strBuf = Space$(255) '//Clear buffer
    lngUser = WNetGetUser("", strBuf, 255)
    If lngUser = NO_ERROR Then
        strUn = Left(strBuf, InStr(strBuf, vbNullChar) - 1)
        Debug.Print "User Name is  " & strUn
    Else
        Debug.Print "Error :" & lngUser
    End If
End Sub




| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
ABOUT
WORKS
THE CELL MASTERS
CONTACT
LINKS
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo