2013-10-25 52 views
0

問題的兩個部分:MSGBOX VBscript的

  1. 我試圖寫一個VBScript,其中一個循環運行,但用戶可以使用在任何時候中止序列的消息框。我知道如果你有一個msgbox的序列,腳本會停止執行,直到收到答案,但是我可以將它作爲下標運行,所以它不會干擾主腳本?

當我用下面的腳本,我從來沒有看到MSGBOX

function test() 
msgbox ("test") 
end function 
wscript.sleep 1000 
msgbox "done 

我是功能讓你輸入的印象。這甚至可以用純vbscript完成嗎?

+0

不完全是,當你調用一個函數時,你的代碼將不會在'main'部分和函數中繼續。您的主進程將跳轉到該函數。 我的做法是這樣做:做你的循環,但在每次迭代檢查是否按下按鈕,如果它是esc提示msgbox問他是否希望退出 –

+0

如何檢查esc鍵是否已被何苦呢? – jeffpkamp

+0

使用keyDown事件 –

回答

0

不是我要去的,但這是我找到的工作。它使一個臨時的msgbox在一段時間後關閉。給用戶一個5秒的窗口來中止每個循環的序列。

set infobox = createobject("Wscript.shell") 
do while E<N+1 
    E=E+1 
    if InfoBox.Popup ("Click cancel to stop sequence", _ 
      5, "Abort Sequence?", 1) = 2 then 
      E=N+1 
    end if 
    loop 
0

這裏的訣竅是讓第一個腳本創建並啓動第二個腳本。這第二個腳本將在後臺運行,然後可以等待並終止初始腳本Process ......這可以通過Function輕鬆完成,並且可以在腳本開始時調用。當您的主腳本結束時,它會簡單地殺死之前創建的第二個腳本。注意:創建的第二個腳本在運行時會自動刪除它自己。請參閱下面的腳本有一個良好的工作示例:

Dim iKillPID 

'Start Kill Script At Start Of Script 
iKillPID = KillPID() 

For X = 10 To 0 Step -1 
    WScript.Echo "Closing in " & X & " Seconds" 
    WScript.Sleep 1000 
Next 

'Kill The Kill Script At End Of Script 
GetObject("winmgmts:root\cimv2:Win32_Process.Handle='" & iKillPID & "'").Terminate 
MsgBox "This Script is Complete" 

'$$$$$$$$$$ 
Function KillPID() 
Dim strKillScriptPath, strKillCommand, KillFile, StrFileKill, iScriptPID 
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") 
    'Generates a Unique Temp File Name In The Same Directory As The Current Script 
strKillScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName) & Chr(92) & Replace(objFSO.GetTempName, ".tmp", ".vbs") 
    'Command Line To New Kill Script 
strKillCommand = "WScript.exe " & Chr(34) & strKillScriptPath & Chr(34) 
    'This part gets the Process ID of the Current Running Script 
iScriptPID = GetObject("winmgmts:root\cimv2:Win32_Process.Handle='" & _ 
CreateObject("WScript.Shell").Exec("CMD /C ping 127.0.0.1 -n 2 > nul").ProcessID & "'").ParentProcessID 
    'String With Kill File Code (Script Process ID Included) 
StrFileKill = _ 
"Const iKillProc = " & iScriptPID & vbCrLf & _ 
"Dim objFSO: Set objFSO = CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & ")" & vbCrLf & _ 
"objFSO.DeleteFile WScript.ScriptFullName, True" & vbCrLf & _ '<-- Deletes itself immediately upon running 
"On Error Resume Next" & vbCrLf & _ 
"Set objKillProc = Nothing" & vbCrLf & _ 
"Set objKillProc = GetObject(" & Chr(34) & "winmgmts:root\cimv2:Win32_Process.Handle='" & Chr(34) & " & iKillProc & " & Chr(34) & "'" & Chr(34) & ")" & vbCrLf & _ 
"If objKillProc Is Nothing Then" & vbCrLf & _ 
" MsgBox " & Chr(34) & "The Process Is Not Running" & Chr(34) & vbCrLf & _ 
" WScript.Quit" & vbCrLf & _ 
"End If" & vbCrLf & _ 
"MsgBox " & Chr(34) & "Click OK To Kill The Script Process" & Chr(34) & vbCrLf & _ 
"Call KillProcess(iKillProc)" & vbCrLf & _ 
"WScript.Quit" & vbCrLf & _ 
"Sub KillProcess(iProcID)" & vbCrLf & _ 
"Dim objKillProc, strParentProc" & vbCrLf & _ 
"On Error Resume Next" & vbCrLf & _ 
"Set objKillProc = Nothing" & vbCrLf & _ 
"Set objKillProc = GetObject(" & Chr(34) & "winmgmts:root\cimv2:Win32_Process.Handle='" & Chr(34) & " & iProcID & " & Chr(34) & "'" & Chr(34) & ")" & vbCrLf & _ 
"If Err = 0 And Not objKillProc Is Nothing Then" & vbCrLf & _ 
" If StrComp(objKillProc.Name, " & Chr(34) & "cmd.exe" & Chr(34) & ", 1) = 0 Or _" & vbCrLf & _ 
"  StrComp(objKillProc.Name, " & Chr(34) & "cscript.exe" & Chr(34) & ", 1) = 0 Or _" & vbCrLf & _ 
"  StrComp(objKillProc.Name, " & Chr(34) & "wscript.exe" & Chr(34) & ", 1) = 0 Then" & vbCrLf & _ 
"  strParentProc = objKillProc.ParentProcessID" & vbCrLf & _ 
"  objKillProc.Terminate()" & vbCrLf & _ 
" Call KillProcess(strParentProc)" & vbCrLf & _ 
" End If" & vbCrLf & _ 
"End If" & vbCrLf & _ 
"Set strParentProc = Nothing" & vbCrLf & _ 
"Err.Clear" & vbCrLf & _ 
"End Sub" 
    'Write the Code To File 
Set KillFile = objFSO.CreateTextFile(strKillScriptPath, True) 
KillFile.WriteLine StrFileKill 
KillFile.Close 
Set KillFile = Nothing 
WScript.Sleep 250 
    'Execute The Script and Return the Script Process ID So You Can Kill It When The Script Ends 
KillPID = CreateObject("WScript.Shell").Exec(strKillCommand).ProcessID 
End Function 
'$$$$$$$$$$ 

而且,如果你使用的CScript的腳本引擎爲您的VBS,我相信你可以通過按CTRL + C在命令提示符窗口停止腳本。

現在,如果你的超級動機可以創建一個HTA,它可以完成同樣的事情,但是可以提供一個UserForm或Custom Internet Explorer窗口來點擊,並且它也可以循環並檢查進程是否仍在運行並關閉自己該腳本完成並且該過程不再運行。你可以添加漂亮的顏色和一切!