通过Excel VBA运行Shell对象/命令时出错

我正在通过VB代码运行一个C ++程序,而且我无法让我的代码在共享驱动器上运行,而在本地计算机上运行。 我的程序生成一组假设,然后通过C ++模型运行这些假设,然后拾取模型输出并准备在VB工作簿中查看。

下面的代码工作正常时,我的工作簿保存在我的C驱动器上的本地目录,但是当我上传到我公司的共享驱动器,我得到以下错误:

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

码:

'--------------------------------------------------------- ' SECTION III - RUN THE MODEL AS C++ EXECUTABLE '--------------------------------------------------------- Dim ModelDirectoryPath As String Dim ModelExecutableName As String Dim ModelFullString As String ' First build the command string Application.StatusBar = "Running C++ Model..." ModelDirectoryPath = Range("ModelFilePath").value ModelExecutableName = Range("ModelFileName").value ModelFullString = ModelDirectoryPath & ModelExecutableName ModelFullString = ModelFullString & " " & ScenarioCounter & " " & NumDeals _ & " " & ModelRunTimeStamp & " " & Settle_YYMMDD ' Run the program Dim wsh As Object Set wsh = VBA.CreateObject("WScript.Shell") Dim waitOnReturn As Boolean: waitOnReturn = True Dim windowStyle As Integer: windowStyle = 1 Dim errorCode As Integer errorCode = wsh.Run(ModelFullString, windowStyle, waitOnReturn) If errorCode = 0 Then ' MsgBox "C++ Model Completed without Errors." Else MsgBox "Program exited with error code " & errorCode & "." End If Application.StatusBar = "C++ Model Complete" 

有什么想法吗?

错误确实来自其中有一个空格的目录:

 C:\Users\myname\this is my folder\myexe.exe 

一个简单的解决方法是诀窍:

 wsh.Run(Chr(34) & YourFullPathDirectoryWithSpaces & "\myexe.exe" & Chr(34)) 

Chr(34)是一个双引号。

.Run采取两线财产有一个问题。

在Excel 2010 / Win32上进行testing。

有同样的问题,替代解决scheme:

 wsh.CurrentDirectory = exePath wsh.Run(exeName & " " & cmdArgs, windowStyle, waitOnReturn) 

关键是要设置shell的CurrentDirectory属性

我已经确定问题完全是由于目录结构包括一个空间。 似乎没有任何有关本地与共享目录的问题。

如果有人知道一个简单的解决方法来解决在其中有一个空间的目录,请让我知道!

我注意到,这个问题似乎只有当多个parameter passing到运行的程序/脚本时才会发生。

如果第二个参数包含带有空格的文件path,则即使存在必需的“”“”,也会发生错误。

在这一点上找不到任何解决scheme…