2016-04-14 32 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_部分存在空格時才接受完整路徑的引號。因此只用空格引用部分會導致錯誤。 –

相關問題