0

I know how to use VBA to run a python script through cmd, and I know how to use VBA to open IDLE, but is it possible to make a module in VBA that will open a python script in IDLE and automatically run that python script? Here's my code for opening IDLE via VBA and cmd (but it doesn't run the script):

Sub runidlefrompython()

Dim args As String

args = "C:\users\opera\AppData\Local\Programs\Python\Python36\Lib\idlelib\idle.py"

Shell "cmd.exe /S /K" & "C:\Users\opera\AppData\Local\Programs\Python\Python36\python.exe" & " " & args & " -r " & "C:\PythonPrograms\Hello.py", vbNormalFocus

End Sub
Tim Stack
  • 3,209
  • 3
  • 18
  • 39
efly
  • 15
  • 5
  • also, would it be possible to add something to the python script itself to make it run automatically when IDLE is opened? – efly Jun 14 '18 at 12:31
  • From [the answer here](https://stackoverflow.com/questions/2148994/when-running-a-python-script-in-idle-is-there-a-way-to-pass-in-command-line-arg), I would say `Shell "cmd.exe /S /K" & "C:\Users\opera\AppData\Local\Programs\Python\Python36\python.exe" & " " & args & " -r " & "C:\PythonPrograms\Hello.py", vbNormalFocus` – Vincent G Jun 14 '18 at 13:20
  • when i do that, i get an "invalid argument" error – efly Jun 14 '18 at 14:06
  • nvm it worked! I just needed to put a space before and after the "-r". Thank you @Vincent – efly Jun 14 '18 at 14:08

1 Answers1

0

Create Example.py with one word only (remove the parenthesis, if you are working with Python2):

print ("Hello World!")

Then try this:

Option Explicit

Sub TestMe()

    Dim args As String
    args = "C:\somePath"
    Shell "cmd.exe /S /K" & args & "\Example.py", vbNormalFocus

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100