VBA 在 OSX 上获取用户名(或 Mac 替代环境(“用户名”))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17847080/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:24:32  来源:igfitidea点击:

VBA Get Username on OSX (or Mac alternative to Environ("username"))

macosvbaexcel-vbaexcel-2010username

提问by ARich

My task has been to create a secure (Windows version) Excel workbook that uses macros to validate whether a user has permission to view the contents. In Excel 2010 for Windows, I use the Environ("username")function to accomplish this.

我的任务是创建一个安全的(Windows 版本)Excel 工作簿,该工作簿使用宏来验证用户是否有权查看内容。在 Excel 2010 for Windows 中,我使用该Environ("username")函数来完成此操作。

The problem I'm running into is that some users in the organization use a Mac to do their daily work, and Environdoes not work with OSX.

我遇到的问题是组织中的一些用户使用 Mac 来完成日常工作,而Environ不能使用 OSX。

Does anyone know of an alternate way to find the username of a Mac? I've looked online but all I can find is how to find the unique identifier of the computer, not the username.

有谁知道找到 Mac 用户名的另一种方法?我在网上查过,但我能找到的只是如何找到计算机的唯一标识符,而不是用户名。

Is what I'm attempting to do even possible with Mac?

我试图用 Mac 做的事情甚至可能吗?

Any information is appreciated!

任何信息表示赞赏!

采纳答案by CuberChase

You can use AppleScript to return the Username and run that from VBA.

您可以使用 AppleScript 返回用户名并从 VBA 运行它。

The following function should do the trick.

以下函数应该可以解决问题。

Function GetUserNameMac() As String
    Dim sMyScript As String

    sMyScript = "set userName to short user name of (system info)" & vbNewLine & "return userName"

    GetUserNameMac = MacScript(sMyScript)
End Function

回答by Jon Peltier

Environ("USER")and Environ("LOGNAME")return the same thing on the Mac as the MacScript answer.

Environ("USER")Environ("LOGNAME")在 Mac 上返回与 MacScript 答案相同的内容。