最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】关于Word VBA中的Environ函数

VBA crifan 3459浏览 0评论

【整理】关于Word VBA中的Environ函数

在别处看到如下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 />&nbsp; Text1.<span>Text</span> = <span style="COLOR: #ff0000;">""</span><br />&nbsp; i = <span style="COLOR: #cc66cc;">1</span><br />&nbsp; <span style="COLOR: #0000ff;">While</span> <span style="COLOR: #0000ff;">Environ</span>$<span style="COLOR: #66cc66;">(</span>i<span style="COLOR: #66cc66;">)</span> &lt;&gt; <span style="COLOR: #ff0000;">""</span><br />&nbsp; &nbsp; 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 />&nbsp; &nbsp; i = i + <span style="COLOR: #cc66cc;">1</span><br />&nbsp; <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函数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
81 queries in 0.229 seconds, using 22.19MB memory