2016-04-14 206 views
1

我试图用输入文件运行程序。VBA Excel命令在用Shell调用时不起作用,但在直接复制粘贴到cmd时起作用

Dim command, cfx5_exe_path, cfx_file_folder, cfx_file_name As String 

command = cfx5_exe_path & " -cfx " & cfx_file_folder & cfx_file_name & ".cfx " 
Shell command 

因此它给出了一个错误。 在调试器中命令的结果值

"c:\"Program Files"\"ANSYS Inc"\v150\CFX\bin\cfx5pre.exe -cfx c:\Users\Username\Arbeit\Programming\A321_tail_flow.cfx" 

如果我是复制粘贴到窗户CMD直接删除第一/最后报价的迹象,那么它完美的作品。 Shell有什么问题?

回答

1

文档说:

If the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.

,它给一个小例子:

Sub test() 
    Dim RetVal 
    Dim command As String 
    command = "C:\WINDOWS\CALC.EXE" 
    RetVal = Shell(command, 1) 
End Sub 

在这里,我得到Error 53: file not found如钙在Windows 7上驻留在别处。你有这个错误吗?

提供正确的calc路径启动程序并返回一个唯一的ID。

然而,引用正确的路径的一部分将抛出错误:

command = "C:\WINDOWS\""SYSTEM32""\CALC.EXE" 

但引用的完整路径并不:

command = """C:\WINDOWS\SYSTEM32\CALC.EXE""" 

所以,你必须删除所有嵌入式引号,然后报价一次完整的路径。

+0

是的,我得到了同样的错误53.你为什么选择system32?我引用了Program Files文件夹名称中有一个空格。 – user3656916

+1

我引用它只是一个尝试。似乎只有在_any_部分存在空格时才接受完整路径的引号。因此只用空格引用部分会导致错误。 –

相关问题