2017-06-20 35 views
1

目前这个脚本运行5分钟,但我需要通过击键来阻止它。使用vbs按键击杀while循环

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

纯VBScript不能做你想做的,但VBA可以([相关](https://stackoverflow.com/q/23725598/1630171))。 –

回答

0

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

也许你能适应这个到您的代码。 这是一种循环方式,直到没有冻结的情况出现。有一件事:你需要使用链接到vbs文件的HTA窗口。

+0

更新的脚本,但给错误变量undefined“Wscript” –

+0

感谢它现在的工作... –