Function to display username/networkname

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've been looking all over and I know it's possible, but I can't seem to
figure this one out.

I want Excel to display the Network name (windows logon ID) in a cell. I'd
like this to be a function, so I can do IF statements with it.

Any help greatly appriciated!
Hendrik
 
Try the following codes
Public Function GetUsername() As String

On Error Resume Next


Dim objScript As Object
Set objScript = CreateObject("WScript.NetWork")
If Not objScript Is Nothing Then
GetUsername = objScript.USERNAME
End If
Set objScript = Nothing


End Function
Public Function GetUserDisplayName() As String
'--------------------8<----------------------
Set oADSystemInfo = CreateObject("ADSystemInfo")
' get user object
Set oADsUser = GetObject("LDAP://" & oADSystemInfo.USERNAME)
' get full name of the current user
GetUserDisplayName = oADsUser.displayname
'--------------------8<----------------------
End Function
 
Thanks Chris, works perfectly!

Chris said:
Try the following codes
Public Function GetUsername() As String

On Error Resume Next


Dim objScript As Object
Set objScript = CreateObject("WScript.NetWork")
If Not objScript Is Nothing Then
GetUsername = objScript.USERNAME
End If
Set objScript = Nothing


End Function
Public Function GetUserDisplayName() As String
'--------------------8<----------------------
Set oADSystemInfo = CreateObject("ADSystemInfo")
' get user object
Set oADsUser = GetObject("LDAP://" & oADSystemInfo.USERNAME)
' get full name of the current user
GetUserDisplayName = oADsUser.displayname
'--------------------8<----------------------
End Function
 
I might be pushing this a bit, but I'd like to record the date and time the
user previously opened the workbook.

A1=The username function you provided.

=IF(A1="Username1",NOW(),"Unknown")

This formula records the time when the file is openend. However, whenever
this user makes a change and the workbook is recalculated, the "NOW" time is
updated. Also, when "Username2" opens the workbook, the formula for
"Username1" will change to "Unkown"

Would you have any thoughts on how to "freeze" the formula?

I understand if this is asking too much :)

Regards,
Hendrik
 
Just another way:

Public Function ReturnNetworkName() As String
ReturnNetworkName = Environ("UserName")
End Function

HTH
 

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

Back
Top