windows - VBA - Windows 和 OSX 中的用户名

标签 windows macos vba excel cross-platform

我正在编写 VBA 代码以从多个 xlsx 文件收集输入并将它们合并到一个主表中。由于此工作表的用户混合了 Windows 和 OSX 用户,因此我的代码需要在两个平台上通用。

我想要实现的其中一件事是确定用户的用户名。这可以在 Windows 上使用 environ("username") 并在 OSX 上使用 MacScript 来完成。但是,以下代码无法在 Mac 上编译,因为它不知道 environ()

OS_IS_MAC = Application.OperatingSystem Like "*Mac*"
If OS_IS_MAC Then
   sMyScript = "set userName to short user name of (system info)" & vbNewLine & "return userName"
   USERNAME = MacScript(sMyScript)

Else

   USERNAME = Environ("UserName")

End If

我该如何解决这个问题?

最佳答案

使用条件编译:

#If Mac Then
   sMyScript = "set userName to short user name of (system info)" & vbNewLine & "return userName"
   USERNAME = MacScript(sMyScript)
#Else
   USERNAME = Environ("UserName")
#End If

关于windows - VBA - Windows 和 OSX 中的用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28872900/

相关文章:

macos - 在 OSX 10.9 上编译 C 包

Excel VBA 按字母顺序循环遍历范围

excel - 如何将 Excel DocumentProperty 对象变量(位于 ByRef 函数中)设置为 WordbuiltinDocumentProperties 集合项?

windows - for 循环不能正常工作?我做错了什么?

c++ - SHGetFolderPath 已弃用 : What is alternative to retrieve path for Windows folders?

windows - 我在哪里保存我的 Haskell "modules"?

vba - 在 Access 中隐藏和重新显示自定义弹出总机?

java - winrun4j 自动检测 JVM

ios - 为什么Swift 3对OSX和iOS版本的NSString会有不同的对待?

macos - CFNetwork SSL NSPOSIXErrorDomain Code=89 on Exchange Web Services