VBScript - 如何让程序等待进程完成? [英] VBScript - How to make program wait until process has finished?

查看:54
本文介绍了VBScript - 如何让程序等待进程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与 VBA/Excel 宏和 HTA 一起使用的 VBScript 中遇到问题.问题只是 VBScript,我还有其他两个组件,即 VBA 宏和 HTA 前端工作完美.但在我解释问题之前,我想为了让你帮助我,我必须帮助你理解 VBScript 的上下文.

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.

因此,基本上所有组件(VBScript、VBA 宏和 HTA)都是我正在构建的工具的一部分,用于自动化一些手动杂务.大致是这样的:

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:

~~~~~~~~~~~~~~~

~~~~~~~~~~~~

  1. 用户从 HTA/GUI 中选择一些文件.
  2. 在 HTA 的 HTML 中,SCRIPT"标签内有一些 VBScript,它将用户 4 个输入文件作为参数传递给 VBScript(由 WScript.exe 执行 - 为了清楚起见,您可以参考注释 #1)
  3. 脚本,从现在起我们称之为 myScript.vbs 然后处理 4 个参数,其中 3 个是特定文件,第 4 个是包含多个文件的路径/文件夹位置 -(为了清楚起见,另请参阅注释 #2)
  1. User selects some files from the HTA/GUI.
  2. 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)
  3. 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

~~~~~~~~~~~~~~~

B - myScript.vbs

~~~~~~~~~~~~

  1. myScript.vbs 打开 Excel 文件的前 3 个参数.其中之一是包含我的 VBA 宏的 *.xlsm 文件.
  2. myScript.vbs 然后使用第四个参数,它是包含多个文件的文件夹的路径,并将其分配给一个变量,以便在调用 GetFolder 时传递给 FileSystemObject 对象,即

  1. myScript.vbs opens up the first 3 arguments which are Excel files. One of them is a *.xlsm file that has my VBA macro.
  2. 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 
    ...
    

  • 请注意,当前 3 个 Excel 文件打开时(由循环之前的代码控制,这里没有显示,因为我对那部分没有问题)我必须让它们保持打开状态.

    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.

    必须依次打开和关闭文件夹中的文件(作为第 4 个参数传递).但是在打开和关闭之间,我需要 VBA/宏(用之前打开的 3 个 Excel 文件之一编写)每次运行循环迭代并从文件夹中打开一个新文件(我希望你关注 - 如果没有请让我知道:)).

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

    我遇到的问题是文件夹中的文件打开和关闭,打开和关闭,n 次(n = 文件夹中的文件数,自然),而无需等待宏运行.这不是我想要的.我在'x1.Run strMyMacro'语句之后尝试了延迟10秒的WScript.sleep语句,但无济于事.

    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.

    有什么想法吗?

    谢谢,八强.

    注意:

    1 - 为简单起见,方法如下:

    1 - For simplicity/clarity this is how:

        strCMD = cmd /c C:windowssystem32wscript.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 HTA 使用一段 JavaScript 剥离用户的第 4 个输入文件(HTML:INPUT TYPE="file")并将其传递给 HTA 内的 VBScript.这让我解决了无法在 HTML 中专门选择文件夹的问题.

    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.

    推荐答案

    你需要告诉 run 等到进程完成.类似的东西:

    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:windowssystem32wscript.exe <path>myScript.vbs " & args
    oShell.Run command, DontShowWindow, WaitUntilFinished
    

    在脚本本身中,像这样启动 Excel.调试开始时可见:

    In the script itself, start Excel like so. While debugging start visible:

    File = "c:	estmyfile.xls"
    oShell.run """C:Program FilesMicrosoft OfficeOffice14EXCEL.EXE"" " & File, 1, true
    

    这篇关于VBScript - 如何让程序等待进程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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