Table of contents
TOC
折叠目录
展开目录

Environ 函数Environ Function

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

返回与操作系统环境变量关联的字符串。在 Macintosh语法上不可用那么 Environ ( { envstring |} 那么 Environ函数语法具有下面这些命名参数Returns the String associated with an operating system environment variable. Not available on the Macintosh SyntaxEnviron( { envstring |number } ) The Environ function syntax has thesenamed arguments:

部件Part说明Description
envstringenvstring可选。字符串表达式,包含环境变量的名称。Optional. String expression containing the name of an environment variable.
编号number可选。对应于环境字符串表中环境字符串的数字顺序的数值表达式Number参数可以是任何数值表达式,但在计算之前四舍五入为整数。Optional. Numeric expression corresponding to the numeric order of the environment string in the environment-string table. The numberargument can be any numeric expression, but is rounded to a whole number before it is evaluated.

备注如果无法找到envstring在环境字符串表中,一个零长度字符串 ("") 将返回。否则,那么 Environ将返回分配给指定envstring; 文本即,文本等号 (=) 环境字符串表中该环境变量中。如果您指定number,将返回占据环境字符串表中的该数值位置的字符串。在这种情况下,那么 Environ将返回的所有文本,包括envstring。如果在指定位置没有环境字符串,那么 Environ将返回一个零长度字符串。Remarks 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.

示例Example

此示例使用那么 Environ函数提供的条目数和长度PATH环境字符串表中的语句。在 Macintosh 上不可用。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.
Do
    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
    Else
        Indx = Indx + 1    ' Not PATH entry,
    End If    ' so increment.
Loop Until EnvString = ""
If PathLen > 0 Then
    MsgBox Msg    ' Display message.
Else
    MsgBox "No PATH environment variable exists."
End If
© 2018 Microsoft