Code Samples for Businesses, Schools & Developers

First Published 3 Oct 2022


The Environ function returns the value of operating system environment variables.

Syntax:
Environ(numeric_value) OR Environ(variable_name)

Examples:
a)   Numeric_value
      Environ(1): "ALLUSERSPROFILE=C:\ProgramData"
      Environ(2): "APPDATA=C:\Users\username\AppData\Roaming"
      Environ(3): "CommonProgramFiles=C:\Program Files (x86)\Common Files"

b)   Variable_name
      Environ("ALLUSERSPROFILE"): "C:\ProgramData"
      Environ("APPDATA"): "C:\Users\username\AppData\Roaming"
      Environ("CommonProgramFiles"): C:\Program Files (x86)\Common Files"



A typical system may have over 40 environment variables.
The following code lists all environment variables to the immediate window in the visual basic editor

Copy the code to a standard module

CODE:

Sub ListEnviron()

'lists all envionment variables
      Dim strg As String
      Dim x As Long

      strg = "Environ Values 1 to 50" & vbCrLf & _
            "===================" & vbCrLf
      For x = 1 To 50
            strg = strg & x & " " & Environ(x) & vbCrLf
      Next x

      Debug.Print strg

End Sub




Feedback

Please use the E-Mail button in the contact form below to let me know whether you found this article useful or if you have any questions.

If you find the article useful, please consider giving a donation to help offset the costs of maintaining my website. Thanks



Colin Riddington           Mendip Data Systems                 Last Updated 3 Oct 2022



Return to Code Samples Page




Return to Top