我正在使用VBA创建一些CSV文件,然后将其用作参考,以便批处理文件可以将它们用作输入。我已经成功完成了这项工作,但发现我需要VBA代码等待脚本完成才能继续。从我在网上找到的,我应该使用Windows脚本主机对象模型“然后允许我设置WaitOnReturn:= True。但是我现在得到”运行时错误'-2147024894(80070002)':自动化错误系统找不到指定的文件。“这显然是我错过的一些基本内容,但我花了几个小时来弄清楚它是什么。我搜索过但没有找到一个似乎有帮助的答案。这是有效的代码,其次是通过失败的代码。提前感谢任何指导。
Public Sub Create_run_delete_batch_file(Input_File)
Const MY_FILENAME = "E:\Test.bat"
Dim FileNumber As Integer
Dim retVal As Variant
FileNumber = FreeFile
'create batch file
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, "E:"
Print #FileNumber, "cd " & Chr(34) & "E:\Test_Dir\" & Chr(34)
'Set Current Risk File here
Print #FileNumber, "Script that works goes here" & Input_File
Close #FileNumber
'run batch file
retVal = Shell(MY_FILENAME, vbNormalFocus)
' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
MsgBox "An Error Occured"
Close #FileNumber
End
End If
'Delete batch file
Kill MY_FILENAME
End Sub
Public Sub Create_run_delete_batch_file(Input_File)
Const MY_FILENAME = "E:\Test.bat"
Dim FileNumber As Integer
Dim retVal As Variant
Dim wsh As WshShell
Set wsh = New WshShell
FileNumber = FreeFile
'create batch file
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, "E:"
Print #FileNumber, "cd " & Chr(34) & "E:\Test_Dir\" & Chr(34)
'Set Current Risk File here
Print #FileNumber, "Script that works goes here" & Input_File
Close #FileNumber
'run batch file
'retVal = Shell(MY_FILENAME, vbNormalFocus)
retVal = wsh.Run(MY_FILENAME, WaitOnReturn:=True)
' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
MsgBox "An Error Occured"
Close #FileNumber
End
End If
'Delete batch file
Kill MY_FILENAME
End Sub