在别处看到如下VBA代码:
Dim sUserEID As String
Dim sHostName As String
Dim sUserName As String
‘ Get Host Name / Get Computer Name
sHostName = Environ$(“computername”)
‘ Get Current User EID
sUserEID = Environ$(“username”)
‘ Get Current User Name
sUserName = Application.UserName
然后得知,可以通过用Environ函数来获得系统的一些环境变量,比如计算机名,用户名等。
但是通过help系统和Object Browser对象浏览器(F8),都无法查到,Environ函数的具体参数有哪些。
到网上找了下,找到了想要的这些参数:
ALLUSERSPROFILE
APPDATA
CI_HOLOS_CLI
CLIENTNAME
CommonProgramFiles
COMPUTERNAME
ComSpec
FP_NO_HOST_CHECK
HOMEDRIVE
HOMEPATH
include
JAVA_HOME
lib
LOGONSERVER
MSDevDir
NUMBER_OF_PROCESSORS
OS
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
ProgramFiles
SESSIONNAME
SystemDrive
SystemRoot
TEMP
TMP
USERDNSDOMAIN
USERDOMAIN
USERNAME
USERPROFILE
windir
通过调用如下VB代码,可以打印出系统中所有的环境变量:
<div style="TEXT-ALIGN: left;" dir="ltr"><div><span style="COLOR: #0000ff;">Dim</span> i <span style="COLOR: #0000ff;">as</span> <span style="COLOR: #0000ff;">Integer</span><br /> Text1.<span>Text</span> = <span style="COLOR: #ff0000;">""</span><br /> i = <span style="COLOR: #cc66cc;">1</span><br /> <span style="COLOR: #0000ff;">While</span> <span style="COLOR: #0000ff;">Environ</span>$<span style="COLOR: #66cc66;">(</span>i<span style="COLOR: #66cc66;">)</span> <> <span style="COLOR: #ff0000;">""</span><br /> Text1.<span>Text</span> = Text1.<span>Text</span> + <span style="COLOR: #0000ff;">Mid</span><span style="COLOR: #66cc66;">(</span><span style="COLOR: #0000ff;">Environ</span>$<span style="COLOR: #66cc66;">(</span>i<span style="COLOR: #66cc66;">)</span>, <span style="COLOR: #cc66cc;">1</span>, <span style="COLOR: #0000ff;">InStr</span><span style="COLOR: #66cc66;">(</span><span style="COLOR: #cc66cc;">1</span>, <span style="COLOR: #0000ff;">Environ</span><span style="COLOR: #66cc66;">(</span>i<span style="COLOR: #66cc66;">)</span>, <span style="COLOR: #ff0000;">"="</span><span style="COLOR: #66cc66;">)</span> - <span style="COLOR: #cc66cc;">1</span><span style="COLOR: #66cc66;">)</span> + <span style="COLOR: #0000ff;">vbNewLine</span><br /> i = i + <span style="COLOR: #cc66cc;">1</span><br /> <span style="COLOR: #0000ff;">Wend</span></div></div>
【引用】
1。Windows Environment Variables in VB
http://www.go4expert.com/forums/showthread.php?t=208
转载请注明:在路上 » 【整理】关于Word VBA中的Environ函数