在VBA中传入多个参数以运行Python脚本

时间:2015-09-01 15:53:25

标签: python vba quotes

我有一个非常简单的VBA脚本,它将运行一个Python脚本,该脚本具有指定的参数(文件夹路径,包含空格)。

代码低于

Dim path As String
path = Left(pdocinfo.path, InStrRev(pdocinfo.path, "\")) ' e.g C:\Temp\City Addresses

Dim pyLocation As String
pyLocation = "C:\Temp\filePath.py"

Dim strReports As String
strReports = path & "Reports\"

Dim strImages As String
strImages = path & "Images\"
Dim shellvar As Long
shellvar = shell("C:\Python26\ArcGIS10.0\python.exe " & pyLocation & " " & path & " " & strImages & " " & strReports , vbNormalFocus)

但是,运行Python脚本时,文件路径(strImagesstrReportspath)无法正确解析,因为它们在文件夹路径中包含空格。 / p>

Python解释多个参数的正确方法是什么,这些参数也是包含空格的动态变量?

编辑: 我在下面的评论中尝试了以下内容

shellvar = Shell("C:\Python27\ArcGIS10.2\python.exe " & pyLocation & " " & """" & path & """" & " " & """" & strImages & """" & " " & """" & strReports & """", vbNormalFocus)

python脚本的结果是 enter image description here

我想要的结果是

Argument 0: C:\Temp\filePath.py
Argument 1: "C:\Temp\City Addresses"
Argument 2: "C:\Temp\City Addresses\Images"
Argument 3: "C:\Temp\City Addresses\Reports"

1 个答案:

答案 0 :(得分:1)

用双引号括起来。

shellvar = shell("C:\Python26\ArcGIS10.0\python.exe " & pyLocation & " " & """" & path & """" & " " & """" & strImages & """" & " " & """" & strReports & """", vbNormalFocus)