使用“WScript.Shell”对象的“运行”方法时运行时错误

我试图通过VBA运行一个命令提示符来执行一些命令,但我遇到了一个错误。

运行时错误'-214702894(80070002)':
对象'IWshShell3'的方法'运行'失败

我已经尝试用引号包装文件path(如下所示),但是错误仍然存​​在。

Shell.Run "cd """ & MyDocumentsPath & """", 1, True Shell.Run "cd " & Chr(34) & MyDocumentsPath & "Chr(34)", 1, True 

我也尝试直接input一个文件path(没有空格),但也失败了。

 Shell.Run "cd ""C:\Users\GardD""", 1, True 

有人能够发现任何问题吗? 这是我的完整代码 –

 Dim Shell As Object ' An instance of the 'Shell' object Set Shell = VBA.CreateObject("WScript.Shell") Dim MyDocumentsPath As String ' The path to the current users 'My Documents' folder MyDocumentsPath = GetMyDocumentsPath Shell.Run "cd " & MyDocumentsPath, 1, True ' Change the Shell start location to the users 'My Documents' folder Shell.Run DIR_CMD & DIR_FILE_PATH, 1, True ' Output the full file paths of all files in the users 'My Documents' folder Set Shell = Nothing ' Destroy the 'Shell' object 

cd不是可执行文件; 它是一个只存在于(cmd.exe)控制台会话中的命令,如果是DIR_CMD的命令,则同上dir

你可以使用Shell.Run "cmd /c cd c:\temp & dir *.xls", 1, true

使用VBA可以更好地在命令行上执行任何你想做的事情。