Environ, Environ$ Function
Returns the string associated with an operating system environment variable.
Environ returns a Variant; Environ$ returns a String.
The Environ[$] function has these parts:
environmentstring String expression containing the name of an environment variable.
n Numeric expression corresponding to the nth string of the environment-string table. The argument n can be any numeric expression, but it is rounded to an integer before it is evaluated.
If the environment variable name you specify cannot be found in the environment-string table, a zero-length String or Variant of VarType Otherwise, Environ[$] returns the text assigned to the specified environment variable (the text following the equal sign in the environment-string table for that environment variable.) 8 is returned.
If you specify a numeric argument (n), the nth string of the environment-string table is returned. In this case, Environ[$] returns all of the text, including environmentstring. If the nth string doesn't exist, Environ[$] returns a zero-length String or Variant of VarType 8.
Environ, Environ$ Functions Example
The example uses Environ to supply the entry number and length of the PATH statement from the environment-string table. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim EnvString, Indx, Msg, PathLen ' Declare variables.
Indx = 1 ' Initialize index to 1.
EnvString = Environ(Indx) ' Get environment variable.
If Left(EnvString, 5) = "PATH=" Then ' Check PATH entry.
PathLen = Len(Environ("PATH")) ' Get length.
Msg = "PATH entry = " & Indx & " and length = " & PathLen
Indx = Indx + 1 ' Not PATH entry, so increment.
Loop Until EnvString = ""
If PathLen > 0 Then
MsgBox Msg ' Display message.
MsgBox "No PATH environment variable exists."