用vbs中的击键杀死while循环

时间:2017-06-20 07:00:00

标签: excel-vba vbscript vba excel

目前这个脚本运行了5分钟,但是我需要通过按键来停止它。

Option Explicit
Dim Excel, x, y, x1, y1, eTime
Set Excel = WScript.CreateObject("Excel.Application")
eTime = DateAdd("n", 2, Now)
x = "111"
y = "222"
x1 = "222"
y1 = "111"
Do While Now < eTime
    Excel.ExecuteExcel4Macro ( _
        "CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & ")")
    WScript.Sleep (1000)
    Excel.ExecuteExcel4Macro ( _
        "CALL(""user32"",""SetCursorPos"",""JJJ""," & x1 & "," & y1 & ")")
  WScript.Sleep (1000)
Loop
WScript.Echo "Program Ended"

我试过下面的代码,但是它没有用,我做错了吗?

Option Explicit
Dim Excel, x, y, x1, y1, eTime
Set Excel = WScript.CreateObject("Excel.Application")
eTime = DateAdd("n", 2, Now)
x = "111"
y = "222"
x1 = "222"
y1 = "111"

Sub Form_KeyPress(KeyAscii As Integer)
    Do While Not KeyAscii = 27
        Excel.ExecuteExcel4Macro ( _
            "CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & ")")
        WScript.Sleep (1000)
        Excel.ExecuteExcel4Macro ( _
            "CALL(""user32"",""SetCursorPos"",""JJJ""," & x1 & "," & y1 & ")")
        WScript.Sleep (1000)
    Loop
    WScript.Echo "Program Ended"

1 个答案:

答案 0 :(得分:0)

我回答了类似的问题How to stop a loop when pressing "OK" on a message box with VBS?

也许您可以根据代码进行调整。 这是一种循环的方式,直到某些东西appen而不冻结。

一件事:您需要使用链接到vbs文件的HTA窗口。