用索引号取得环境变量 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 NextEnd Sub
它的一个输出例子:ALCKPath=C:\Program Files\Lenovo\AutoLockALLUSERSPROFILE=C:\ProgramDataAPPDATA=C:\Users\fcai\Application DataCommonProgramFiles=C:\Program Files (x86)\Common FilesCommonProgramFiles(x86)=C:\Program Files (x86)\Common FilesCommonProgramW6432=C:\Program Files\Common FilesCOMMPath=C:\Program Files\Lenovo\Communications UtilityCOMPUTERNAME=ComSpec=C:\Windows\system32\cmd.execonfigsetroot=C:\Windows\ConfigSetRootFP_NO_HOST_CHECK=NOHOMEDRIVE=C:HOMEPATH=LOCALAPPDATA=LOGONSERVER=NUMBER_OF_PROCESSORS=4OS=Windows_NTPath= PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSCPROCESSOR_ARCHITECTURE=x86PROCESSOR_ARCHITEW6432=AMD64PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntelPROCESSOR_LEVEL=6PROCESSOR_REVISION=3a09ProgramData=C:\ProgramDataProgramFiles=C:\Program Files (x86)ProgramFiles(x86)=C:\Program Files (x86)ProgramW6432=C:\Program FilesPSModulePath=READYAPPS=C:\SWTOOLS\ReadyAppsSESSIONNAME=ConsoleSystemDrive=C:SystemRoot=C:\WindowsTEMP=TMP=USERDNSDOMAIN=USERDOMAIN=USERNAME=USERPROFILE=WecVersionForRosebud.28E0=3windir=C:\Windows
用环境变量名获取环境变量值 sub getComputerName() msgbox vba.environ("computername")end sub