Environ Function

Note: The function, method, object, or property described in this topic is disabled if the Microsoft Jet Expression Service is running in sandbox mode, which prevents the evaluation of potentially unsafe expressions. For more information on sandbox mode, search for "sandbox mode" in Help.

Returns the String associated with an operating system environment variable. Not available on the Macintosh


Environ( { envstring | number } )

The Environ function syntax has these arguments:




Optional. String expression containing the name of an environment variable.


Optional. Numeric expression corresponding to the numeric order of the environment string in the environment-string table. The number argument can be any numeric expression, but is rounded to a whole number before it is evaluated.


If envstring can't be found in the environment-string table, a zero-length string ("") is returned. Otherwise, Environ returns the text assigned to the specified envstring; that is, the text following the equal sign (=) in the environment-string table for that environment variable.

If you specify number, the string occupying that numeric position in the environment-string table is returned. In this case, Environ returns all of the text, including envstring. If there is no environment string in the specified position, Environ returns a zero-length string.


Note: Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.

This example uses the Environ function to supply the entry number and length of the PATH statement from the environment-string table. Not available on the Macintosh.

Dim EnvString, Indx, Msg, PathLen    ' Declare variables.
Indx = 1 ' Initialize index to 1.
' Get environment variable.
EnvString = Environ(Indx)
' Check PATH entry.
If Left(EnvString, 5) = "PATH=" Then
' Get length.
PathLen = Len(Environ("PATH"))
Msg = "PATH entry = " & Indx & " and length = " _
& PathLen
Exit Do
' Not PATH entry, so increment.
Indx = Indx + 1
End If
Loop Until EnvString = ""
If PathLen > 0 Then
' Display message.
MsgBox Msg
MsgBox "No PATH environment variable exists."
End If
Share Facebook Facebook Twitter Twitter Email Email

Was this information helpful?

Great! Any other feedback?

How can we improve it?

Thank you for your feedback!