Excel VBA中的Shell调用冻结 - 涉及MacScript和Python(Excel for Mac 2011)
这个宏在Mac的Excel 2011上运行得很好:
Sub Works()
scriptToRun = "do shell script ""/anaconda/bin/python -c "" & quoted form of ""import test_file"" "
res = MacScript(scriptToRun)
Debug.Print res
End Sub
对应的 test_file.py
文件(把它放在pythonpath的某个地方)是:
import sys
print(sys.version)
但是,当我使用 appscript
这个Python包时,它会长时间卡住,什么都不做。把 test_file.py
文件改成这样(先运行 pip install appscript
):
import appscript
app = appscript.app('Microsoft Excel')
app.cells['A1'].value.set(2)
不过,当我直接从AppleScript编辑器运行这个命令时,它是可以工作的:
do shell script "/anaconda/bin/python -c " & quoted form of "import test_file"
我该如何在Mac的Excel VBA中调用这个呢?(顺便说一下,在Windows上,用 WScript.Shell
和 pywin32
很容易就能做到)
1 个回答
0
解决办法是使用 system()/popen()
这个调用,具体可以参考这个 回答。然后在命令的最后加上一个“&”符号,这样就可以把这个命令放在后台运行了:
Private Declare Function system Lib "libc.dylib" (ByVal command As String) As Long
Sub RunTest()
Dim result As String
result = system("/anaconda/bin/python -c 'import test_file' &")
End Sub