Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Excel user's name 13

Status
Not open for further replies.

pho01

Programmer
Mar 17, 2003
218
US
How do I get the username's of the Excel application using VBA?

To be more clear, that's the username when one brings up Excel application, "this product is licensed to ...(username)".

Thanks,
 
Hi
This should be the one but be aware that this is easy for the user to change thru tools>options>general.

Code:
Application.UserName

Dunno how this works on a network where the corp would be the licensee.....

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
As Loomah says Username can easily be changed so perhaps the login name may be more useful?

Try this macro

Option Explicit

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, _
ByVal lpUserName As String, _
lpnLength As Long) As 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 WinUsername()
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)
MsgBox "User Name is " & strUn
Else
MsgBox "Error :" & lngUser
End If
End Sub

HTH
 
Thank you all. It was helpful, more than what it expects it.
 
just another (shorter) way
myUser = environ("username")

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Geoff . . . You 'da MAN!!! [thumbsup2]

I have been using the same solution that Chattin suggested above forever and never even thought that there might be another way of doing this!!!

A big fat
star.gif
from me!!!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Of course . . . NOW you have to tell us all where you got this lovely little tid-bit and if there are any other uses of the environ() operator.

;-)



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Picked up the code a coupla years ago from an excel developers list that I subscribe to. There are indeed other "environ" functions. Run this to find 'em out:

Sub env()
Dim EnvString As String
Indx = 1
Do
EnvString = Environ(Indx)
Cells(Indx, 1) = EnvString
Indx = Indx + 1
Loop Until EnvString = ""
End Sub

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Hi Geoff,

I've often wondered what other environ variables were available. Now I know how to find out. Star from me too.

Enjoy,
Tony
 
Hi Geoff,

Elegant solution. I thought 5 stars was the limit, your already at 5, I gave you one anyway, and it appeared to take. Is it really going to take?

If not I owe you one!!!

Steve
 
Thanks steve - 5 is the visual limit - AFAIK, any amount of stars can be awarded for a post but I think this is the 1st time I've reached the visual limit - woo yay !!

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Used environ("username") a hundred times and never thought to post it here!!!

Just a little addition for anyone still taking any notice - you can also get a list of the available environment variables through DOS by using the 'Set' command/keyword.

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Couldn't resist One More Star. I guess 5 is the Max.

Arun

"Whereever you go there are people who need you for what you can do..."
 
Geoff,
Nice Env() routine!!! Thanks for the Tip! A
star.gif
from me too. [thumbsup2]

********************
What's the best way to get the answers you need?? See FAQ222-2244 for details!
 
Does environ("username") work in XL97? It returns blank for me (although the little SUB above does list out 9 other environ() variables that are available to me).

VBAjedi [swords]
 
environ("username") does work in xl97 but not on all OS's - I used to think it only worked on NT but have since moved to XP and it still works. It may be that it doesn't work for your particular OS......

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Set WshNetwork = WScript.CreateObject("WScript.Network")
strUserName = WshNetwork.UserName

'if it doesnt work for your OS try the above, you might
'need to use CreateObject rather than Wscript.CreateObject
 
Geoff,
Just found this Thread whilst doing a keyword search for username, your code

myUser = environ("username")

Has saved me some work trying to figure out how to get the username on "workbook open".

Thanks

Regards

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top