Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBA Tips - Código de exemplo para recuperar o nome de usuário atual

7 views
Skip to first unread message

André Luiz Bernardes

unread,
May 20, 2012, 11:37:45 AM5/20/12
to

See more in: http://inanyplace.blogspot.com.br/2012/05/vba-tips-codigo-de-exemplo-para.html

Este exemplo de código, aplicado ao Microsoft Visual Basic for Applications usa a função WNetGetUser (Função API do Windows no arquivo MPR.dll) para recuperar o nome de usuário usado para estabelecer uma conexão de rede.

' Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0 'The Function call was successful
Sub GetUserName()
' Buffer size for the return string.
Const lpnLength As Integer = 255
' Get return buffer space.
Dim status As Integer
' For getting user information.
Dim lpName, lpUserName As String

' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)
' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)
' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else
' An error occurred.
MsgBox "Unable to get the name."
End
End If
' Display the name of the person logged on to the machine.
MsgBox "The person logged on this machine is: " & lpUserName
End Sub
0 new messages