WScript.Shell crashes Excel



我正在使用WshShell从Excel中的VBA运行批处理文件。

  • 批处理文件非常简单,其中一行运行数字运算程序我可以直接运行批处理文件而不会出现问题
  • 我正在使用WshShell或WScript.Shell,因为我希望VBA等待加载计算完成后,结果将返回Excel
  • 我引用了";Microsoft Scripting Runtime";以及";"Windows脚本主机对象模型">
  • 我已经使用相同的代码很长时间了直到2-3周前它还很好现在它关闭Excel。如果我打开了一些电子表格,它会将它们全部关闭
  • 我有两个版本,但它们都在";设置WinSh="线Excel不是响应几(~10(秒,然后关闭
  • 我使用的是Office 365 MSO(16.0.12527.21294(32位。它由我的组织管理
  • 我在Word中尝试了相同的代码,但它也崩溃了

有人知道最近的更新是否能做到这一点吗?或者我可以使用的另一种方法?

潜艇为:

Public Sub RunBatch(FPath As String)
Dim WinSh As Object
Dim StrCmd As String
Dim ErrCode As Long
Set WinSh = New WshShell
StrCmd = Chr(34) & FPath & Chr(34)
ErrCode = WinSh.Run(StrCmd, WindowStyle:=1, WaitOnReturn:=True)
End Sub

Public Sub RunBatch2(FPath As String)
Dim WinSh As Object
Set WinSh = VBA.CreateObject("WScript.Shell")
WinSh.Run FPath, 1, True
End Sub

问题的根源是Cisco AMP安全策略的更新或更改。这会阻止使用Windows脚本主机。我所在组织的安全部门正在努力寻找解决这个问题的办法。

与此同时,我找到了一种解决方法。我使用shell(在恢复VBA代码之前不等待批处理结束(来运行批处理,该批处理在进程结束时写入一个简单的文本文件,并等待文本文件以1秒为增量出现。它很粗糙,但到目前为止还有效。

Public Sub OneRunBatch()
Dim xlWB As Workbook
Dim fso1 As New FileSystemObject
Dim BatFile As Object
Dim IsDone As Boolean
Dim OutFileName As String
Dim DoneFileName As String
Dim OutPath As String
Dim DeleteBatFile As Boolean
Set xlWB = ThisWorkbook
OutPath = xlWB.Path
OutFileName = "HW.bat"
DoneFileName = "Done.txt"
DeleteBatFile = False
Set BatFile = fso1.CreateTextFile(OutPath & "" & OutFileName)
BatFile.WriteLine "cd /d " & OutPath
BatFile.WriteLine "echo Hello World"
BatFile.WriteLine "dir > " & DoneFileName
BatFile.Close
IsDone = False
Call Shell(OutPath & "" & OutFileName, vbNormalFocus)
Do Until IsDone
If fso1.FileExists(OutPath & "" & DoneFileName) Then IsDone = True
Application.Wait (Now + TimeValue("00:00:01"))
Loop
fso1.DeleteFile (OutPath & "" & DoneFileName)
If DeleteBatFile Then fso1.DeleteFile (OutPath & "" & OutFileName)
End Sub

最新更新