Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 6 of 6

Thread: WinNT Userid Function

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Calgary
    Posts
    57
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.

  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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)
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  3. #3
    Board Regular
    Join Date
    Feb 2002
    Location
    Calgary
    Posts
    57
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.

  4. #4
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Portland, OR Area - USA
    Posts
    1,607
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I pasted it into my workbook and it worked also.

  5. #5
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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:



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



    HTH

    _________________
    [b] Mark O'Brien

    [ This Message was edited by: Mark O'Brien on 2002-02-26 13:21 ]

  6. #6
    Board Regular
    Join Date
    Feb 2002
    Location
    Calgary
    Posts
    57
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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:



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



    HTH

    _________________
    [b] Mark O'Brien

    [ This Message was edited by: Mark O'Brien on 2002-02-26 13:21 ]
    Thanks Mark...this now works!!

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •