WinNT Userid Function

jpettit

Board Regular
Joined
Feb 20, 2002
Messages
57
I am trying to use this piece of code:
http://www.chrisrae.com/vba/routines/getusername.html

' By Chris Rae, 14/6/99, 3/9/00.
Option Explicit
' This is used by GetUserName() to find the current user's
' name from the API
Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetUserName() As String
Dim lpBuff As String * 25

Get_User_Name lpBuff, 25
GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
End Function

When i try to access it by calling the GetUserName, i am getting an error stating:
"Compile Error: Constants, Fixed-Length Strings, Arrays, and Declare statements are not allowed as Public members of object modules"

Can someone give me a heads up?
Thanks.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Works fine for me. Did you paste it in an empty module ? The "Option Explicit" and "Declare Function" should be at the top (If you're not pasting to an empty module)
 
Upvote 0
I tried using it in a clean workbook...pasted it in and then tried to compile it...same error.
The Option and Declare are in the (Declarations) section at the top.
 
Upvote 0
On 2002-02-26 11:33, jpettit wrote:
I am trying to use this piece of code:
http://www.chrisrae.com/vba/routines/getusername.html

' By Chris Rae, 14/6/99, 3/9/00.
Option Explicit
' This is used by GetUserName() to find the current user's
' name from the API
Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetUserName() As String
Dim lpBuff As String * 25

Get_User_Name lpBuff, 25
GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
End Function

When i try to access it by calling the GetUserName, i am getting an error stating:
"Compile Error: Constants, Fixed-Length Strings, Arrays, and Declare statements are not allowed as Public members of object modules"

Can someone give me a heads up?
Thanks.

I get the feeling that this code is not in a standard module. You need to change the API declaration to be "Private" so the First Declaration should look like this:<pre>

' name from the API
Private Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long</pre>

HTH

_________________<font color = green> Mark O'Brien
This message was edited by Mark O'Brien on 2002-02-26 13:21
 
Upvote 0
On 2002-02-26 13:21, Mark O'Brien wrote:
I get the feeling that this code is not in a standard module. You need to change the API declaration to be "Private" so the First Declaration should look like this:<pre>

' name from the API
Private Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long</pre>

HTH

_________________<font color = green> Mark O'Brien
This message was edited by Mark O'Brien on 2002-02-26 13:21


Thanks Mark...this now works!!
 
Upvote 0

Forum statistics

Threads
1,223,320
Messages
6,171,434
Members
452,402
Latest member
siduslevis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top