2013-05-07 68 views
0

我通过VBA宏启动Python。如果Python路径不好,我想要一个适当的错误消息。 我在2台不同的计算机上使用此宏,并且这两台计算机上的Python路径不一样。 2.路径是2个型动物细胞名path_Python和path_Python_2 这里是我的代码:VBA错误处理,将错误定义为错误

Function launchPython() As Boolean 
Dim pathScript As String 

pathScript = [path_files] + "code.py" 

On Error GoTo err 
    Shell [path_Python].Value & " " & pathScript 
    launchPython = True 
    Exit Function 

err: 
    On Error GoTo err2 
     Shell [path_Python_2].Value & " " & pathScript 
     launchPython = True 
err2: 
    launchPython = False 
    MsgBox "Error: please check Python paths" 

End Function 

问题是,当2路并不好,而不是去ERR2,我有一个VBA错误信息阻塞

err: 
    On Error GoTo err2 
     Shell [path_Python_2].Value & " " & pathScript 

我该如何解决这个问题? 非常感谢你

+0

你不能使用IF语句而不是处理程序来验证路径吗? – 2013-05-07 09:24:50

回答

0

而不是试图命令一个可能丢失的路径命令,我会检查一个文件存在并尝试。您可以使用http://allenbrowne.com/func-11.html中的FileExists等许多函数之一,或者只检查dir $是否返回非空白。

Dim Path as string 

path = path_python 

if dir(path) = "" then 
    path = path_python2 
    if dir(path) = "" then err.raise 
end if 

shell path & " " & pathScript