VBA中如何用environ$ 或 environ方法取得环境变量?

时间:2024-04-16 09:35:19
用索引号取得环境变量
Sub
EnumSEVars() Dim strVar As String Dim i As Long For i = 1 To 255 strVar = Environ$(i) If LenB(strVar) = 0& Then Exit For Debug.Print strVar Next End Sub
它的一个输出例子:
ALCKPath=C:\Program Files\Lenovo\AutoLock
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\fcai\Application Data
CommonProgramFiles=C:\Program Files (x86)\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMMPath=C:\Program Files\Lenovo\Communications Utility
COMPUTERNAME=
ComSpec=C:\Windows\system32\cmd.exe
configsetroot=C:\Windows\ConfigSetRoot
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=
LOCALAPPDATA=
LOGONSERVER=
NUMBER_OF_PROCESSORS=4
OS=Windows_NT
Path= 
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_ARCHITEW6432=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3a09
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PSModulePath=
READYAPPS=C:\SWTOOLS\ReadyApps
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=
TMP=
USERDNSDOMAIN=
USERDOMAIN=
USERNAME=
USERPROFILE=
WecVersionForRosebud.28E0=3
windir=C:\Windows
 
用环境变量名获取环境变量值
sub
getComputerName() msgbox vba.environ("computername") end sub