[英]Error running Shell object / commands through Excel VBA
I am running a C++ program through my VB code, and I am having trouble getting my code to run on a shared drive vs. on a local computer.我正在通过我的 VB 代码运行 C++ 程序,但我无法让我的代码在共享驱动器上与在本地计算机上运行。 My program generates a set of assumptions, then runs those assumptions through a C++ model, then picks up the model output and prepares it for viewing in the VB workbook.我的程序生成一组假设,然后通过 C++ 模型运行这些假设,然后获取模型输出并准备在 VB 工作簿中查看。
The code below works fine when I have the workbook saved in a local directory on my C drive, but when I upload it to my company's shared drive, I get the following error:当我将工作簿保存在 C 盘上的本地目录中时,下面的代码可以正常工作,但是当我将其上传到公司的共享驱动器时,出现以下错误:
"Run-time error '-2147024894 (80070002)': Method 'Run' of object 'IWshShell3' failed" “运行时错误‘-2147024894 (80070002)’:对象‘IWshShell3’的方法‘运行’失败”
Code:代码:
'---------------------------------------------------------
' 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"
The error does come from the directory having a space in it: 该错误确实来自其中有空格的目录:
C:\Users\myname\this is my folder\myexe.exe
A simple workaround does the trick: 一个简单的解决方法是诀窍:
wsh.Run(Chr(34) & YourFullPathDirectoryWithSpaces & "\myexe.exe" & Chr(34))
Chr(34)
is a double quote. Chr(34)
是双引号。
There was an issue with .Run
taking a two line property. 有一个问题.Run
采取两行属性。
Tested it on Excel 2010/Win32. 在Excel 2010 / Win32上测试过它。
Had the same problem, alternative solution: 有同样的问题,替代解决方案:
wsh.CurrentDirectory = exePath
wsh.Run(exeName & " " & cmdArgs, windowStyle, waitOnReturn)
Key point being to set the CurrentDirectory property of the shell 关键是要设置shell的CurrentDirectory属性
I have determined that the problem appears entirely due to the directory structure including a space. 我已经确定问题出现完全是由于包含空格的目录结构。 There does not appear to be any issue relating to the local vs. shared directory. 似乎没有任何与本地与共享目录相关的问题。
If someone knows an easy fix to get around the directory having a space in it, please let me know! 如果有人知道一个简单的解决办法来绕过有空格的目录,请告诉我!
I have noticed that the problem only seems to occur when more than one argument is passed on to the program/script that is run. 我注意到,只有当多个参数传递给运行的程序/脚本时,才会出现问题。
If the second argument contains a filepath with a space, even when the necessary """" are present, the error occurs. 如果第二个参数包含带空格的文件路径,即使存在必要的“”“”,也会发生错误。
Can't find any solution at this point... 此时找不到任何解决方案......
found this great reference explaining triple quotes when there is a space in the path. 发现这个伟大的参考资料解释了路径中有空格时的三重引号。 Here is a portion of it in case it gets taken down or moves: 如果它被取下或移动,这是它的一部分:
if you had wanted to open a file 如果你想打开一个文件
c:\\My Files\\Text File.txt
and your shell required that this be wrapped in""
, then you'd write the string like thisc:\\My Files\\Text File.txt
和你的shell要求用""
包装,然后你要写这样的字符串
PID = Shell("notepad ""c:\\My Files\\Text File.txt""", vbNormalFocus)
The same goes for any path you need to specify for the actual command/script name. 对于您需要为实际命令/脚本名称指定的任何路径,情况也是如此。 In this example I'm calling a batch file (Text Parser.bat) to process the text file (Text File.txt): 在这个例子中,我正在调用一个批处理文件(Text Parser.bat)来处理文本文件(Text File.txt):
PID = Shell("""c:\\My Scripts\\Text Parser.bat"" ""c:\\My Files\\Text File.txt""", vbNormalFocus)
All of those
"""
look a bit strange but let me explain. The first and last"
mark the beginning and end of the string that specifies the program being called, including any parameters, switches and file(s) it will use: 所有这些"""
看起来有点奇怪,但让我解释。第一个和最后一个"
标记字符串的开头和结尾,指定被调用的程序,包括它将使用的任何参数,开关和文件:
"""c:\\My Scripts\\Text Parser.bat"" ""c:\\My Files\\Text File.txt"""
If we remove those"
we are left with the Program string itself, which is composed of two separate strings, one for the path\\batch file, and the other for the path\\file the batch file will use ."""c:\\My Scripts\\Text Parser.bat"" ""c:\\My Files\\Text File.txt"""
如果我们删除那些"
我们留下了程序字符串本身,它由两个独立的组成字符串,一个用于路径\\批处理文件,另一个用于批处理文件将使用的路径\\文件。
""c:\\My Scripts\\Text Parser.bat"" ""c:\\My Files\\Text File.txt""
When this is passed to the Shell one of the double"
is removed so what is actually seen in the Shell is""c:\\My Scripts\\Text Parser.bat"" ""c:\\My Files\\Text File.txt""
当这个传递给Shell时,其中一个双"
被删除,所以在Shell中实际看到了什么是
"c:\\My Scripts\\Text Parser.bat" "c:\\My Files\\Text File.txt"
Which looks like two normally delimited strings."c:\\My Scripts\\Text Parser.bat" "c:\\My Files\\Text File.txt"
这看起来像两个通常分隔的字符串。
This thread is ancient but I ran across the same issue and devised a solution.这个线程很古老,但我遇到了同样的问题并设计了一个解决方案。
Write some code to put that command into a batch file in C:\\Temp... that way Shell or Wscript.Shell will receive a digestible "C:\\Temp\\thing.bat" as the argument.编写一些代码将该命令放入 C:\\Temp... 中的批处理文件中,这样 Shell 或 Wscript.Shell 将接收可消化的“C:\\Temp\\thing.bat”作为参数。
Worked like a charm for me.对我来说就像一个魅力。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.