0

I have a simple Python script ("hi.py") as below

import time

print("HI")
time.sleep(5)

I am using Excel VBA to call ""hi.py" by Windows Script Host's Run Method (code as below). It works.

sub testRun()
    Dim wsh As Object
    Dim a As Variant

    Set wsh = CreateObject("WScript.Shell")
    a = wsh.run("C:\tmpFolder\pyProject\myProject\hi.py", 1, True)
End Sub

But if I use Windows Script Host's Exec Method rather than Run Method, my computer say hi.py is not a valid win 32 application.

Sub testExec()
    Dim wsh As Object, oexec As Object

    Set wsh = CreateObject("WScript.Shell")
    Set oexec = wsh.exec("C:\tmpFolder\pyProject\myProject\hi.py")
End Sub

How can I run my python script with Windows Script Host Exec Method? Thank you very much.

PaichengWu
  • 2,649
  • 1
  • 14
  • 28
  • `wsh.exec` requires a binary executable file. A Python file is neither binary nor executable. There exist methods to compile as Python file into an EXE. Use Google to find them. – DYZ Jul 19 '17 at 02:53
  • @DYZ thanks. I will Google something like `py2exe`. – PaichengWu Jul 19 '17 at 09:33
  • Take a look at this: https://stackoverflow.com/questions/45410316/is-there-a-way-to-call-a-python-code-in-excel-vba – Uri Goren Jul 31 '17 at 15:11

1 Answers1

0

The Method exec() needs a String that can be passed to the command line. Try python C:\tmpFolder\pyProject\myProject\hi.py or python3 C:\tmpFolder\pyProject\myProject\hi.py, just like you would type it in CMD.exe

Link to the Syntax at MSDN

ata
  • 3,398
  • 5
  • 20
  • 31