2017-06-20 56 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

感謝它現在的工作... –