I have a working macro that runs a Python script. I need to move the folder to my OneDrive which includes a space in the file path. I've looked at other examples and tried putting triple quotes around the file paths, but it's still not working.
Macro that works when there are no spaces in the file path is below.
Sub runpython3()
Dim Ret_Val
Dim args As String
'Change directory to current directory and save Workbook
folder_path = Application.ThisWorkbook.Path
ChDir (folder_path)
ThisWorkbook.Save
'PythonExe and script path.
args = folder_path & "\job_sim_2.py"
PythonExe = folder_path & "\..\venv\scripts\python.exe"
'Run Python script
Ret_Val = Shell("cmd /k " & PythonExe & " " & args, vbNormalFocus)
If Ret_Val = 0 Then
MsgBox "Couldn't run python script!", vbOKOnly
End If
End Sub
Macro that contains space in file path. The triple quotes did not fix the issue.
Sub runpython3()
Dim Ret_Val
Dim args As String
'Change directory to current directory and save Workbook
folder_path = Application.ThisWorkbook.Path
ChDir (folder_path)
ThisWorkbook.Save
'PythonExe and script path. NOTE: To use folder_path, can not contain spaces in path
args = """C:\Users\smalik\OneDrive - NI\Job-Sim\source\job_sim_2.py"""
PythonExe = """C:\Users\smalik\OneDrive - NI\Job-Sim\venv\Scripts\python.exe"""
'Run Python script
Ret_Val = Shell("cmd /k " & PythonExe & " " & args, vbNormalFocus)
If Ret_Val = 0 Then
MsgBox "Couldn't run python script!", vbOKOnly
End If
End Sub
I get 2 errors when running the Macro
- Macro fails on ChDir (folder_path). If I comment out this line I get the second error
- CMD errors on the file path due to the space.
enter image description here