我想使用excel VBA在python中编辑脚本文件,然后在ArcGis中运行。首先,我可以采用任何更简单的方式,但到目前为止,我无法使用我的VBA sub制作多行脚本!
& vbCrLf &
无法在Fileout.Write
命令中工作。当我打开我的脚本文件时,一切都在同一行。
& Char(34) &
都没有工作。
Private Sub CommandButton4_Click()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim path As String
path = Application.ActiveWorkbook.path
Dim Fileout As Object
Set Fileout = fso.CreateTextFile(path & "\" & "test2.py", True, True)
Fileout.Write "import arcpy & vbCrLf & lyr = arcpy.mapping.Layer( & Char(34) & limits & Char(34) &) & vbCrLf & lyr.visible = True & vbCrLf & arcpy.RefreshActiveView()'"
Fileout.Close
End Sub
答案 0 :(得分:4)
错误在于:
Fileout.Write "import arcpy & vbCrLf & lyr = arcpy.mapping.Layer( & Char(34) & limits & Char(34) &) & vbCrLf & lyr.visible = True & vbCrLf & arcpy.RefreshActiveView()'"
这不会给出预期的输出,因为您在要评估的VBA代码周围有引号。修正引号
Fileout.Write "import arcpy" & vbCrLf & "lyr = arcpy.mapping.Layer(" & Char(34) & limits & Char(34) & ")" & vbCrLf & "lyr.visible = True" & vbCrLf & "arcpy.RefreshActiveView()"
让我们使用一些续行(_
)来使这段代码更清晰:
Fileout.Write "import arcpy" & vbCrLf _
& "lyr = arcpy.mapping.Layer(" & Char(34) & limits & Char(34) & ")" & vbCrLf _
& "lyr.visible = True" & vbCrLf _
& "arcpy.RefreshActiveView()"
Char(34)
可以通过双引号完成(引号内的""
计算为双引号):
Fileout.Write "import arcpy" & vbCrLf _
& "lyr = arcpy.mapping.Layer(""" & limits & """)" & vbCrLf _
& "lyr.visible = True" & vbCrLf _
& "arcpy.RefreshActiveView()"
您可以使用多个WriteLine
代替Write
删除每行& vbCrLf _
:
Fileout.WriteLine "import arcpy"
Fileout.WriteLine "lyr = arcpy.mapping.Layer(""" & limits & """)"
Fileout.WriteLine "lyr.visible = True"
Fileout.WriteLine "arcpy.RefreshActiveView()"
答案 1 :(得分:0)
这就是:你在正确的轨道上,但在处理字符串建设时只是偶然发现。
你想连接双引号括起来的字符串,使用&运营商。当你需要一个字符串中的双引号时,只需加倍它们。
Private Sub CommandButton4_Click()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim path As String
path = Application.ActiveWorkbook.path
Dim Fileout As Object
Set Fileout = fso.CreateTextFile(path & "\" & "test2.py", True, True)
Fileout.Write "import arcpy" & vbCrLf & "lyr = arcpy.mapping.Layer(""limits"")" & vbCrLf & "lyr.visible = True" & vbCrLf & "arcpy.RefreshActiveView()'"
Fileout.Close
End Sub