I am looking for some information or code samples for the Environ
Function in VBA to grab the username on the current system.
This question is related to
vba
environment-variables
Environ()
gets you the value of any environment variable. These can be found by doing the following command in the Command Prompt:
set
If you wanted to get the username, you would do:
Environ("username")
If you wanted to get the fully qualified name, you would do:
Environ("userdomain") & "\" & Environ("username")
Some time when we use Environ()
function we may get the Library or property not found error. Use VBA.Environ()
or VBA.Environ$()
to avoid the error.
As alluded to by Eric, you can use environ with ComputerName argument like so:
MsgBox Environ("USERNAME")
Some additional information that might be helpful for you to know:
Sub EnumSEVars()
Dim strVar As String
Dim i As Long
For i = 1 To 255
strVar = Environ$(i)
If LenB(strVar) = 0& Then Exit For
Debug.Print strVar
Next
End Sub
Source: Stackoverflow.com