通过Excel VBA运行Shell对象/命令时出错 [英] Error running Shell object / commands through Excel VBA

查看:1362
本文介绍了通过Excel VBA运行Shell对象/命令时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



代码:

 '----------------- ---------------------------------------- 
'第三部分 - 运行型号为C ++ EXECUTABLE
'----------------------------------------- ----------------
Dim ModelDirectoryPath As String
Dim ModelExecutableName As String
Dim ModelFullString As String

'First构建命令字符串
Application.StatusBar =运行C ++模型...

ModelDirectoryPath = Range(ModelFilePath)。value
ModelExecutableName = Range(ModelFileName) .value
ModelFullString = ModelDirectoryPath& ModelExecutableName
ModelFullString = ModelFullString& &情景摄影& NumDeals _
& & ModelRunTimeStamp& & Settle_YYMMDD

'运行程序
Dim wsh As Object
设置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)

如果errorCode = 0然后
'MsgBoxC ++模型完成没有错误。
Else
MsgBox程序退出,错误代码& errorCode&
如果

Application.StatusBar =C ++模型完成

任何想法?

解决方案

错误 来自具有空格的目录:

  C:\Users\myname\this是我的文件夹\myexe.exe 

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

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

Chr(34)是双引号。



有一个问题使用 .Run 采取两行属性。



在Excel 2010 / Win32上测试。


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. 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.

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:

"Run-time error '-2147024894 (80070002)': Method 'Run' of object 'IWshShell3' failed"

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"

Any thoughts?

解决方案

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.

There was an issue with .Run taking a two line property.

Tested it on Excel 2010/Win32.

这篇关于通过Excel VBA运行Shell对象/命令时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆