Environ, Environ$ Function


Returns the string associated with an operating system environment variable.

Syntax 1


Syntax 2



Environ returns a Variant8PHEAW3; Environ$ returns a String.

The Environ[$] function has these parts:

Part                           Description


environmentstring        String expression1330R89 containing the name of an environment variable.

n                                 Numeric expression71RISN 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 VarType7A68ZTZ 8 is returned.  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.)

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

          Exit Do


          Indx = Indx + 1                 ' Not PATH entry, so increment.

      End If

   Loop Until EnvString = ""

   If PathLen > 0 Then

      MsgBox Msg                         ' Display message.


      MsgBox "No PATH environment variable exists."

   End If

End Sub