Using an API Declaration

Using an API is no different than calling a function or procedure you created in VBA. The following example uses the GetUserName declaration in a function to return the UserName in Excel:

Public Function UserName() As String Dim sName As String * 256 Dim cChars As Long cChars = 256

If GetUserName(sName, cChars) Then

End Function

Sub ProgramRights() Dim NameofUser As String

NameofUser = UserName

Select Case NameofUser

Case Is = "Administrator"

MsgBox "You have full rights to this computer" Case Else

MsgBox "You have limited rights to this computer" End Select

End Sub

Run the ProgramRights macro and you will learn whether you are currently signed on as the administrator or not. The result shown in Figure 22.1 indicates an administrator sign-on.

0 0

Post a comment