【发布时间】:2021-01-31 06:45:42
【问题描述】:
我正在使用来自here 的 Excel vba execShell 脚本在 Excel for Mac 中使用 curl 从 Web 服务器中提取数据。它在我自己的 Mac(以及我一直在测试的许多其他 Mac)上运行良好。
但是,在朋友 Mac(相同(最新)版本的 Mac OS 和 Excel 365)上,它会失败。似乎在他的 Mac 上 execShell 返回空字符串。
我简化了脚本,只运行echo 命令并体验相同的行为。这是我正在运行的测试脚本
Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr
Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long
Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr
Function execShell(ByVal command As String, Optional ByRef exitCode As Long) As String
Dim file As LongPtr
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function
Sub test_command()
Dim errorCode As Long
result = execShell("echo Value_returned", errorCode)
msgbox(result & " " & errorCode)
End Sub
在我的 Mac 上,test_command 显示
Value_returned
0
在他的 Mac 上只显示
0
result = "" 返回True。
任何想法可能导致这种行为?
任何不需要更新我的朋友操作系统的解决方法?
非常感谢。
【问题讨论】:
标签: excel vba macos popen libc