Who Am I?

R

Ross Culver

Could someone please tell me how I can find out what my network login name
is in VBA?

I do it all the time in .net, but there's no 'Import' in vba and I haven't a
clue what to reference to be able to get the current user's network name
(not the application username).

I thought I almost had it with:

Set orootDSE = GetObject("LDAP://rootDSE")
MsgBox orootDSE.get("currentuid")

but 'currentuid' isn't correct.

Thanks,

Exasperated
 
C

Chip Pearson

Ross,

You can use a function like the following:

Function MyUserName() As String
MyUserName = Environ("UserName")
End Function

I have seen on rare occassion the Environ return an empty string. The
following code is a wee bit more complex but will always work.

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

Function MyUserNameAPI() As String
Dim UName As String
Dim L As Long
Dim R As Long
L = 255
UName = String$(L, vbNullChar)
R = GetUserName(UName, L)
MyUserNameAPI = Left(UName, L - 1)
End Function


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
J

JW

Could someone please tell me how I can find out what my network login name
is in VBA?

I do it all the time in .net, but there's no 'Import' in vba and I haven't a
clue what to reference to be able to get the current user's network name
(not the application username).

I thought I almost had it with:

Set orootDSE = GetObject("LDAP://rootDSE")
MsgBox orootDSE.get("currentuid")

but 'currentuid' isn't correct.

Thanks,

Exasperated

Try:
Environ("username")

or:
http://www.exceltip.com/st/Function...er_name_using_VBA_in_Microsoft_Excel/452.html
 
S

Steve Yandl

See if this works.

_____________________________

Set objNetwork = CreateObject("WScript.Network")
strUser = objNetwork.UserName
MsgBox strUser
______________________________

Steve
 
M

marwan_hefnawy

You can use WMI as following

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")

Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
Wscript.Echo "Logged-on user: " & objComputer.UserName
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top