Excel/VBA 环境(“用户名”)错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39656844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel/VBA Environ("username") error
提问by John
I have a spreadsheet that contains a range of model inputs that get loaded into a SQL Server database. Part of this process involves building an audit trail of who updated what and when - I record the user and machine name as well as timestamp.
我有一个电子表格,其中包含加载到 SQL Server 数据库中的一系列模型输入。这个过程的一部分涉及建立一个关于谁更新了什么以及何时更新的审计跟踪——我记录了用户和机器名称以及时间戳。
Within my VBA code, I have the following:
在我的 VBA 代码中,我有以下内容:
user = VBA.environ$("username")
user = VBA.environ$("用户名")
This is running on a Win7 machine, with Office 2013.
这是在装有 Office 2013 的 Win7 机器上运行的。
When I run the code, it all works fine, but when someone in production runs it (on the same machine, but they are logged in, not me), it falls over on the line above. I have used variations of the above (user = environ("username"), user = environ$("username")) but always with the same outcome - it works for me, but not for others.
当我运行代码时,一切正常,但是当生产中的某人运行它时(在同一台机器上,但他们已登录,而不是我),它会落在上面的行上。我已经使用了上述的变体(用户 = 环境(“用户名”),用户 = 环境 $(“用户名”))但总是具有相同的结果 - 它适用于我,但不适用于其他人。
Does anyone have any thoughts on how to fix this?
有没有人对如何解决这个问题有任何想法?
采纳答案by omegastripes
Here you are:
这个给你:
Sub Test()
With CreateObject("WScript.Network")
Debug.Print .UserName
Debug.Print .ComputerName
Debug.Print .UserDomain
End With
End Sub
回答by Siddharth Rout
The environment variables are unreliable
环境变量不可靠
- The user can edit the value to anything they want
- The user can delete the environment variables.
- 用户可以将值编辑为他们想要的任何内容
- 用户可以删除环境变量。
Try one of these APImethods.
尝试这些API方法之一。
Option 1
选项1
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Sub Sample()
Dim lpBuff As String * 25
Dim ret As Long, UserName As String
ret = GetUserName(lpBuff, 25)
UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
MsgBox UserName
End Sub
Option 2
选项 2
Option Explicit
Private Declare Function GetEnvironmentVariable Lib _
"kernel32" Alias "GetEnvironmentVariableA" _
(ByVal lpName As String, ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
Private Sub Sample()
Dim strUserName As String * 255
Dim x As Integer
x = GetEnvironmentVariable("USERNAME", strUserName, Len(strUserName))
If x > 0 Then
x = InStr(strUserName, vbNullChar)
If x > 0 Then
MsgBox (Left$(strUserName, x - 1))
Else
MsgBox (Left$(strUserName, x))
End If
End If
End Sub
回答by Doug Coats
I always use
我总是用
Environ("USERPROFILE")
But generally speaking I need to reference that person's desktop quite a bit.
但一般来说,我需要参考那个人的桌面。
回答by Jeremy
I always use Application.UserName
because it's almost always better - environ$("username")
could give you something like "john~smith" whereas Application.UserName
would give you "John Smith"
我总是使用Application.UserName
因为它几乎总是更好 -environ$("username")
可以给你类似“john~smith”的东西,而Application.UserName
会给你“John Smith”