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

bre*_*ntf 12 shell excel vba

我正在通过我的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"
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

小智 13

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

C:\Users\myname\this is my folder\myexe.exe
Run Code Online (Sandbox Code Playgroud)

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

wsh.Run(Chr(34) & YourFullPathDirectoryWithSpaces & "\myexe.exe" & Chr(34))
Run Code Online (Sandbox Code Playgroud)

Chr(34) 是双引号.

有有一个问题,.Run采取两行属性.

在Excel 2010/Win32上测试过它.


bre*_*ntf 0

我确定问题的出现完全是由于包含空格的目录结构造成的。本地目录与共享目录似乎不存在任何问题。

如果有人知道解决包含空格的目录的简单方法,请告诉我!