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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing ADVAPI32.DLL from Excel VBA

Status
Not open for further replies.
Jun 12, 2003
8
US
I am using some code I found for accessing the computer name and user name of the person using the Excel worksheet.

In using the code below I get an error message the the "user-defined type is not defined." I believe it is because I am not referencing the library properly and can't find refrence to it in the VBA editor.

Thanks,
Darin

Private Declare Function GetComputerName Lib "kernel32" _
Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) _
As Long

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

Public Function NameOfComputer()
' Returns the name of the computer
Dim ComputerName As String
Dim ComputerNameLen As Long
Dim Result As Long
ComputerNameLen = 256
ComputerName = Space(ComputerNameLen)
Result = GetComputerName(ComputerName, ComputerNameLen)
If Result <> 0 Then
NameOfComputer = Left(ComputerName, ComputerNameLen)
Else
NameOfComputer = "Unknown"
End If
End Function

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

 
Hi jdteichmer,

I don't believe that message is a symptom of a problem with advapi32.dll. I also don't see anything else in your code which would be likely to cause it. How are you calling (or trying to call) these functions?

Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
I have placed this code in a module, I am then calling it from code in a form using:

txtRequester = UserName

Debug shows that the error comes when trying to decalre the function.
 
Hi jdteichmer,

UserName is a Function. To invoke it as a Function it needs to be followed by its parameters enclosed in parentheses. Even when there are no parameters, the (empty) parentheses are still required, so try using ..

Code:
[blue]txtRequester = UserName[highlight]()[/highlight][/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
You could also probably use the ENVIRON fuunction for this

userID = environ("username")
compID = environ("computername")

works with NT / XP for sure - havn't had any other OS's to test on though

Rgds, Geoff
[blue]Experience is something you don't get until just after you need it[/blue]
We want to help [red]you[/red] Help us by reading this FAQ 1st faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top