Environ, Environ$ Functions

Named Arguments

Yes

Syntax

Environ({envstring | number})

envstring

Key: Optional

Data Type: String

The name of the required environment variable.

number

Key: Optional

Data Type: Numeric expression

The ordinal number of the environment variable within the environment string table.

Return Value

Environ returns a string containing the text assigned to envstring.

Description

Returns the value assigned to an operating-system environment variable.

Rules at a Glance

  • A zero-length string ("") is returned if envstring doesn't exist in the operating system's environment-string table, or if there is no environment string in the position specified by number.

  • envstring and number are mutually exclusive; that is, you can specify one or the other, but not both.

Example

Private Type env
   strVarName As String
   strValue As String
End Type

Private Sub Form_Load()

Dim intCtr As Integer, intPos As Integer
Dim strRetVal As String
Dim udtEnv As env

intCtr = 1
Do
   strRetVal = Environ(intCtr)
   If strRetVal <> "" Then
      intPos = InStr(1, strRetVal, "=")
      udtEnv.strVarName = Left(strRetVal, intPos - 1)
      udtEnv.strValue = Mid(strRetVal, intPos + 1)
   Else
      Exit Do
   End If
   intCtr = intCtr + 1
Loop

End Sub

Programming Tips and Gotchas

  • If you use number to specify the environment variable, both the name and the value of the variable are returned. An equals sign (=) separates them. For example, the function call Environ(1) might return the string TEMP=C:\WINDOWS\TEMP.

  • If you retrieve environment ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.