VBA Excel命令在使用Shell调用时不能工作,但在直接复制粘贴到cmd时可以工作

时间:2022-09-02 15:55:37

I'm trying to run a program with an input file.

我正在尝试用一个输入文件运行一个程序。

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

so it gives an error. the resulting value of the command in the debugger is

它会产生误差。调试器中命令的结果值为

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

If I copy-paste that into windows cmd directly and remove first/last quotation signs, then it works perfectly. What is the problem with Shell?

如果我直接将它粘贴到windows cmd中,并删除第一个/最后一个引号,那么它就能很好地工作。Shell的问题是什么?

1 个解决方案

#1


1  

The documentation says:

文档表示:

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.

如果Shell函数成功执行了命名文件,它将返回已启动程序的任务ID。任务ID是一个唯一的数字,用于标识正在运行的程序。如果Shell函数不能启动指定的程序,就会发生错误。

and it gives a small example:

它给出了一个小例子:

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

Here I get Error 53: file not found as calc on Windows 7 resides somewhere else. Do you get this error?

这里我得到了错误53:在Windows 7中没有找到calc的文件位于其他地方。你有这个错误吗?

Providing the right path to calc starts the program and returns a unique ID.

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

However, quoting a part of the correct path will throw the error:

但是,引用正确路径的一部分会抛出错误:

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

but quoting the full path does not:

但引用完整的路径并不意味着:

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

So you must remove all embedded quotes and then quote the full path once.

因此,您必须删除所有嵌入的引号,然后引用完整路径一次。

#1


1  

The documentation says:

文档表示:

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.

如果Shell函数成功执行了命名文件,它将返回已启动程序的任务ID。任务ID是一个唯一的数字,用于标识正在运行的程序。如果Shell函数不能启动指定的程序,就会发生错误。

and it gives a small example:

它给出了一个小例子:

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

Here I get Error 53: file not found as calc on Windows 7 resides somewhere else. Do you get this error?

这里我得到了错误53:在Windows 7中没有找到calc的文件位于其他地方。你有这个错误吗?

Providing the right path to calc starts the program and returns a unique ID.

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

However, quoting a part of the correct path will throw the error:

但是,引用正确路径的一部分会抛出错误:

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

but quoting the full path does not:

但引用完整的路径并不意味着:

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

So you must remove all embedded quotes and then quote the full path once.

因此,您必须删除所有嵌入的引号,然后引用完整路径一次。