VBScript - 如何使程序等待,直到进程完成? [英] VBScript - How to make program wait until process has finished?
问题描述
所以,基本上所有的组件(VBScript,VBA宏和HTA)是我正在建立的工具的一部分,以自动化一些手工作业。这很像这样:
A - HTA
~~~~~~~~~~~ ~~
- 用户从HTA / GUI中选择一些文件。
- HTA在SCRIPT标签中有一些VBScript,它将用户4个输入文件作为参数传递给VBScript(由WScript.exe执行 - 您可以在这里参考注释1)
- 脚本,让我们从现在开始调用它的myScript.vbs ,然后处理4个参数,其中3个是特定文件,第4个是路径/文件夹位置,其中包含多个文件 - (另请参阅注释2以了解)
B - myScript.vbs
~~~~~~~~~~~~
- myScript.vbs打开前3个参数,这些参数是Excel文件。其中一个是一个* .xlsm文件,具有我的VBA宏。
-
myScript.vbs然后使用第四个参数,它是一个包含多个文件和分配的文件夹的PATH一个变量在调用GetFolder时传递给FileSystemObject对象,即
...'其他代码在这里,与此无关post
Dim FSO,FLD,strFolder
...'其他代码在这里,不相关的这篇文章
arg4 = args.Item(3)
strFolder = arg4
设置FSO = CreateObject(Scripting.FileSystemObject
'获取对要搜索的文件夹的引用
设置FLD = FSO.GetFolder(strFolder)
...
-
从这里我创建一个循环,以便我可以顺序打开文件夹
中的文件,然后运行我的宏,即...
Dim strWB4,strMyMacro
strMyMacro =Sheet1.my_macro_name
'循环通过文件夹并获取文件名
对于每个Fil在FLD.Files
S et x4WB = x1.Workbooks.Open(Fil)
x4WB.Application.Visible = True
x1.Run strMyMacro
x4WB.close
下一页
...
请注意当前3个Excel文件已打开(由循环前的代码控制,并且在此处未显示,因为该部分没有问题),我必须保持打开。
文件夹中的文件(作为第四个参数传递)必须依次打开和关闭。但是在打开和关闭之间,我需要VBA /宏(在之前打开的3个Excel文件之一中写入)每次运行循环循环并从文件夹打开一个新文件(我希望你以下 - 如果不是,请让我知道:))。
我遇到的问题是文件夹中的文件打开和关闭,打开和关闭,n个次(n =文件夹中的文件数量,自然),而不等待宏运行。这不是我想要的。我已经在'x1.Run strMyMacro'语句之后延迟了10秒的时间尝试了WScript.sleep语句,但没有用。
任何想法?
谢谢,
QF。
注意:
1 - 为了简单起见,这是如何:
strCMD = cmd / c C:\windows\system32\ wscript.exe myScript.vbs< arg1> < ARG2> < ARG3> < ARG4>
'FYI - 这是通过创建一个WShell对象,wsObj,并使用.run方法,即WShell.run(strCMD)
2 HTA使用一段JavaScript来剥离用户第四个输入文件(HTML:INPUT TYPE =file),并将其传递给HTA中的VBScript。这得到了我无法在HTML中唯一选择FOLDER的问题。
你需要告诉运行等待进程完成。如下所示:
const DontWaitUntilFinished = false,ShowWindow = 1,DontShowWindow = 0,WaitUntilFinished = true
set oShell = WScript.CreateObject(WScript.Shell)
command =cmd / c C:\windows\system32\wscript.exe< path> \myScript.vbs& args
oShell.Run命令,DontShowWindow,WaitUntilFinished
在脚本本身中,启动Excel,所以。调试开始时可见:
File =c:\test\myfile.xls
oShell.run C:\Program Files\Microsoft Office\Office14\EXCEL.EXE&文件,1,真
I have a problem in a VBScript that I am using with a VBA/Excel macro and a HTA. The problem is just the VBScript, I have the other two components, i.e. the VBA macro and HTA front-end working perfectly. But before I explain the problem, I think for you to help me I must help you understand the context of the VBScript.
So, basically all components (VBScript, VBA macro and HTA) are parts of a tool that I am building to automate some manual chores. It pretty much goes like this:
A - HTA
~~~~~~~~~~~~
- User selects some files from the HTA/GUI.
- Within the HTML of the HTA there is some VBScript within the "SCRIPT" tags which passes the users 4 input files as arguments to a VBScript (executed by WScript.exe - you may refer to note #1 for clarity here)
- The script, lets call it myScript.vbs from now on then handles the 4 arguments, 3 of which are specific files and the 4th is a path/folder location that has multiple files in it - (also see note #2 for clarity)
B - myScript.vbs
~~~~~~~~~~~~
- myScript.vbs opens up the first 3 arguments which are Excel files. One of them is a *.xlsm file that has my VBA macro.
myScript.vbs then uses the 4th argument which is a PATH to a folder that contains multiple files and assigns that to a variable for passing to a FileSystemObject object when calling GetFolder, i.e.
... 'Other code here, irrelevant for this post Dim FSO, FLD, strFolder ... 'Other code here, irrelevant for this post arg4 = args.Item(3) strFolder = arg4 Set FSO = CreateObject("Scripting.FileSystemObject" 'Get a reference to the folder you want to search Set FLD = FSO.GetFolder(strFolder) ...
From here I create a loop so that I can sequentially open the files within the folder and then run my macro, i.e.
... Dim strWB4, strMyMacro strMyMacro = "Sheet1.my_macro_name" 'loop through the folder and get the file names For Each Fil In FLD.Files Set x4WB = x1.Workbooks.Open(Fil) x4WB.Application.Visible = True x1.Run strMyMacro x4WB.close Next ...
Please note that when the first 3 Excel files have opened (controlled by code prior to the loop, and not shown here as I am having no problem with that part) I must keep them open.
It is the files in the folder (that was passed as the 4th argument) which must sequentially open and close. But inbetween opening and closing, I require the VBA/macro (wrote in one of the 3 Excel files previously opened) to run each time the loop iterates and opens a new file from the folder (I hope you follow - if not please let me know :) ).
The problem I am having is that the files in the folder open and close, open and close, n number of times (n = # of files in folder, naturally) without waiting for the macro to run. This is not what I want. I have tried the WScript.sleep statement with a 10 second delay after the 'x1.Run strMyMacro' statement, but to no avail.
Any ideas?
Thanks, QF.
NOTES:
1 - For simplicity/clarity this is how:
strCMD = cmd /c C:\windows\system32\wscript.exe myScript.vbs <arg1> <arg2> <arg3> <arg4>
'FYI - This is run by creating a WShell object, wsObj, and using the .run method, i.e. WShell.run(strCMD)
2 The HTA employs a piece of JavaScript that strips the users 4th input file (HTML: INPUT TYPE="file") and passes that to the the VBScript within the HTA. This gets me round the problem of not being able to exclusively select a FOLDER in HTML.
You need to tell the run to wait until the process is finished. Something like:
const DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0, WaitUntilFinished = true
set oShell = WScript.CreateObject("WScript.Shell")
command = "cmd /c C:\windows\system32\wscript.exe <path>\myScript.vbs " & args
oShell.Run command, DontShowWindow, WaitUntilFinished
In the script itself, start Excel like so. While debugging start visible:
File = "c:\test\myfile.xls"
oShell.run """C:\Program Files\Microsoft Office\Office14\EXCEL.EXE"" " & File, 1, true
这篇关于VBScript - 如何使程序等待,直到进程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!