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!

Need library reference

Status
Not open for further replies.

bpeirson

Technical User
Sep 28, 2005
85
CA
I have the following code in VBA Excel. The "GetUserName" statement returns the "Can't find project or library" error.

This code runs well on several machines here. I only have a problem with the newest machine. I hope that all I need to do is find the appropriate library and reference it. if anyone could tell me which library that is I would be greatful.

Code:
Public Function NameOfUser() As String
'   Returns the name of the logged-in user
    Dim Buffer As String * 100
    Dim BuffLen As Long
    BuffLen = 100
    GetUserName Buffer, BuffLen
    NameOfUser = Left(Buffer, BuffLen - 1)
End Function

In case it becomes an issue the new machine is the only machine running Windows Vista.

An alternate method to do the same thing would also help me out.

Thanks
 
Why not simply use this ?
NameOfUser = Environ("username")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PH has a good suggestion.

In any case, GetUserName uses an API that you have to declare.
Code:
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


Sub GetName()
MsgBox GetUserName
End Sub
works for me.

Not sure about:
Code:
NameOfUser = Left(Buffer, BuffLen - 1)
Does not look right to me.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks all. I forgot the declaration when I copied the function from one project to another.

I'm not entirely certain about every line of the code is or does, I found it on Eng-tips a few years ago.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top