Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

The problem I'm running into is that some users in the organization use a Mac to do their daily work, and Environ does not work with 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.

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

Any information is appreciated!

like image 544
ARich Avatar asked Jul 25 '13 00:07

ARich


People also ask

What is Environ username?

Environ("username") is the user name which you log into Windows with. Application.UserName is the user name which is set by the user when they run the program for the first time or whatever it was if you go to TOOLS > OPTIONS > GENERAL and set it under User Name.

How do I find the username of a macro?

We have created “GetComputerUserName” macro to find the computer and user name. We can run the macro by clicking “Submit” button. Macro will extract computer name in cell C10 and user name in cell C11. Environ function returns the string value associated with environment variable of the operating system.


1 Answers

You can use AppleScript to return the Username and run that from 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
like image 103
CuberChase Avatar answered Sep 30 '22 02:09

CuberChase