gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:14:32 25 4
gpt4 key购买 nike

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

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

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

代码:

'---------------------------------------------------------
' 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 上进行了测试。

关于excel - 通过 Excel VBA 运行 Shell 对象/命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21267283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com