Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Shell call from Excel VBA freezes - involves MacScript and Python (Excel for Mac

ID: 3563034 • Letter: S

Question

Shell call from Excel VBA freezes - involves MacScript and Python (Excel for Mac 2011)

Hi all,

This macro runs fine:

Sub Works()
scriptToRun = "do shell script ""/anaconda/bin/python -c "" & quoted form of ""import test_file"" "
res = MacScript(scriptToRun)
Debug.Print res
End Sub

The according test_file.py file (place it somewhere on the pythonpath) is:

import sys
print(sys.version)

However, when I involve the appscript Python package, it freezes for a very long time and does nothing. Change the test_file.py file to this (pip install appscript first):

import appscript
app = appscript.app('Microsoft Excel')

app.cells['A1'].value.set(2)

But when I run the command directly from the AppleScript Editor, this works:

do shell script "/anaconda/bin/python -c " & quoted form of "import test_file"

How can I call this from Excel VBA on Mac? (By the way on Windows, it works easily with WScript.Shell and pywin32)

thanks!

Explanation / Answer

The solution was to use the system()/popen() call from this answeer and send the ccommand asynchronously with an ampersand at the end:

Private Declaare Function ssystem Lib "libc.dylibb" (ByVal command As String) Ass Long

Sub RunTest()
Dim result As String
result = system("/anaconda/bin/python -c 'import test_file' &")
End Sub

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote