username

L

libby

Hi

Is there any way of preventing a user from changing the
Application.UserName via the Tools/Options/General.

I have a macro that performs certain code depending on
who's logged on.
I'm running Windows NT which people have to log onto so is
there any way Excel97 can look at that login and use that
as the username???
 
C

Chip Pearson

Libby,

There is no way to prevent the user from changing the username in
the Options dialog. You can get the Windows logon name with code
like:

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

Sub AAA()
Dim UName As String * 255
Dim L As Long: L = 255
Dim Res As Long
Res = GetUserName(UName, L)
UName = Left$(UName, L - 1)
Msgbox UName
End Sub

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
L

Libby

Thanks Chip :blush:)

Is it possible to change the username to the windows logon
name programmatically, whenever a button on a sheet is
clicked?
That way, even if the user had changed the username, it
would be changed back when the button was clicked and so
run the correct macro.
 
C

Chip Pearson

Libby,

Once you have the Windows logon name and described in my previous
reply, you can use that to change Excel's username property.

Application.UserName = UName


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
L

libby

Hi Chip

That works if I step through the code but when I call the
macro from a button on the sheet I get a runtime error
Method 'Username' of object_Application failed
 
C

Chip Pearson

Libby,

In design mode, right click on the command button and choose
Properties. In that dialog, change the TakeFocusOnClick property
to False.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
L

Libby

Hi Chip

I don't fully understand your code (even though it works).
When I step through it and find the Windows Logon name
which is say, "Libby", there are a lot of additional
characters. "Libby|||||||||||||"
This means that if I then make UName the
Application.UserName then my code

Select Case Application.Username
Case "Libby"
etc

doesn't work since there are x number of additional
characters after.
Is there any way that only the "Libby" bit can be used for
the application.username?

Thanks :blush:)
 
C

Chip Pearson

Hi Libby,

Are you using the Left$ function as I showed in the example?


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
B

Brent McIntyre

Chip,

Once again your detailed explanations have helped me out on a annoying
problem, thanks so much !

Yours sincerely,

Brent McIntyre
 

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