我正在尝试使用VBA脚本运行python脚本。但是,我认为VBA在解析python和python脚本的文件路径时遇到了困难。请在下面找到我的代码:
Sub RunPythonScript()
Dim shell As Object
Dim exePath As String, scriptPath As String
Dim waitOnReturm As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Set shell = VBA.CreateObject("Wscript.Shell")
exePath = """C:\Users\Dhruva Rao\AppData\Local\Programs\Python\Python39\python.exe"""
scriptPath = """G:\Shared drives\Project Analysis Team\Model v3\Codebase\single_site\main.py"""
shell.Run "cmd /k" & " " & exePath & " " & scriptPath
End Sub
以下是cmd提示的屏幕截图:错误信息截图
感谢你对这里出了什么问题的任何反馈。
发布于 2021-07-30 11:10:45
尝尝这个
exePath = "C:\Users\" & chr(34) & "Dhruva Rao" & chr(34) & "\AppData\Local\Programs\Python\Python39\python.exe""
发布于 2021-07-30 11:10:23
你看到的问题是路径中有空格。我在下面的代码中对此进行了更正并进行了调整,所以我使用chr(34)代替使用chr(34),后者在字符串中插入引号(")-character。(我更喜欢这种方法,因为它清楚地表明了引号的意图。)
但是,一旦修复了这个问题,您也会遇到cmd问题--您的shell的一部分。shell本身与cmd相同,因此通过选择shell.run(),您实际上已经启动了cmd提示符,因此需要指定运行哪个程序(python.exe)。
Sub RunPythonScript()
Dim shell As Object
Dim exePath As String, scriptPath As String
Dim waitOnReturm As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Set shell = VBA.CreateObject("Wscript.Shell")
exePath = "C:\Users\Dhruva Rao\AppData\Local\Programs\Python\Python39\python.exe"
scriptPath = "G:\Shared drives\Project Analysis Team\Model v3\Codebase\single_site\main.py"
shell.Run Chr(34) & exePath & Chr(34) & " " & Chr(34) & scriptPath & Chr(34)
End Sub
https://stackoverflow.com/questions/68595746
复制相似问题