【问题标题】:How to add a msgbox with start and end button to start and exit the loop in Vbscript如何在 Vbscript 中添加带有开始和结束按钮的 msgbox 以开始和退出循环
【发布时间】:2016-12-26 11:10:09
【问题描述】:

我编写了一个 VB 脚本代码来模拟使用 do 循环的按键。该程序在指定的时间间隔后模拟键盘的击键事件。我需要在脚本中添加一个消息框,以便在工作结束时将其关闭。目前,它在后台无限运行,直到您注销系统。

这个循环应该在后台运行,直到有人使用 MsgBox 中的是/否按钮手动结束它,或者请建议任何其他方式来关闭这个脚本。

这是我写的代码:

Set Wshell=CreateObject("Wscript.shell")
Do
Wshell.SendKeys "{SCROLLLOCK}"

WScript.sleep 10000
Loop

我也尝试过使用 select 语句,但它似乎不起作用。

【问题讨论】:

    标签: vba excel vbscript


    【解决方案1】:

    您可以尝试类似的方法来询问停止脚本的问题:

    Option Explicit
    Dim Title,Ws
    Title = "Ask a question to stop the script !"
    Set Ws=CreateObject("Wscript.shell")
    Do
        Ws.SendKeys "{SCROLLLOCK}"
        WScript.sleep 10000
        Call Ask_Question()
    Loop
    
    Sub Ask_Question()
        Dim Answer
        Answer=MsgBox("Did you want to stop this script ?"_
        & vbcr & "( Yes / No ) ?",vbQuestion+vbYesNo,Title)
            If Answer=vbYes Then
                Wscript.Quit(0)
            Else 
                Exit Sub    
            End If      
    End Sub
    

    编辑于 19/08/2016 @ 12:53

    只是一个一般的例子:

    因为,我不知道你监控的是什么程序,所以我计划以Notepad.exe 为例 这个脚本可以检查程序Notepad.exe是否正在运行 如果不是这样,问你是否停止脚本!

    Option Explicit
    Dim ProcessPath,WshShell
    ProcessPath = "%Windir%\System32\Notepad.exe"
    Set WshShell = CreateObject("WScript.Shell")
    If AppPrevInstance() Then 
        MsgBox "There is an existing proceeding !" & VbCrLF &_
        CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
        WScript.Quit   
    Else 
        Do  
            Call Main()
            Pause(10) ' Pause 10 seconds 
            If CheckProcess(DblQuote(ProcessPath)) = False Then
                Call Ask_Question()
            End If  
        Loop
    End If
    '**************************************************************************
    Function CheckProcess(ProcessPath)
        Dim strComputer,objWMIService,colProcesses,Tab,ProcessName
        strComputer = "."
        Tab = Split(ProcessPath,"\")
        ProcessName = Tab(UBound(Tab))
        ProcessName = Replace(ProcessName,Chr(34),"")
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set colProcesses = objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name = '"& ProcessName & "'")
        If colProcesses.Count = 0 Then
            CheckProcess = False
        Else
            CheckProcess = True
        End if
    End Function
    '**************************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '**************************************************************************
    Sub Pause(Secs)    
        Wscript.Sleep(Secs * 1000)    
    End Sub   
    '**************************************************************************
    Function AppPrevInstance()   
        With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
            With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
                " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
                AppPrevInstance = (.Count > 1)   
            End With   
        End With   
    End Function    
    '***************************************************************************
    Function CommandLineLike(ProcessPath)   
        ProcessPath = Replace(ProcessPath, "\", "\\")   
        CommandLineLike = "'%" & ProcessPath & "%'"   
    End Function
    '****************************************************************************
    Sub Main()
       WshShell.SendKeys "{SCROLLLOCK}"
    End Sub
    '****************************************************************************
    Sub Ask_Question()
        Dim Answer,Title
        Title = "Ask a question to stop the script !"
        Answer=MsgBox("Did you want to stop this script ?"_
        & vbcr & "( Yes / No ) ?",vbQuestion+vbYesNo,Title)
            If Answer=vbYes Then
                Wscript.Quit(0)
            Else 
                Exit Sub    
            End If      
    End Sub
    '****************************************************************************
    

    【讨论】:

    • 感谢您的回复,但循环应该继续运行而不询问用户是否要继续,这是脚本的唯一目的。我想要可以手动停止脚本的 msgbox(或任何其他方式)。
    • @Ansh 那么在这种情况下,您应该描述停止脚本的条件是什么?我的意思是你什么时候想在什么工作之后停止脚本?
    • 我实际上在后台运行我系统上的另一个程序(自动化脚本)。该脚本只是在其他程序在后台运行时保持屏幕打开。我想在自动化测试完成后手动停止这个脚本。您可以建议任何可以阻止它的方法就足够了。
    • @Ansh 用一个通用示例检查我最后的编辑代码并从中获得灵感!
    【解决方案2】:

    它使您能够在下一次按下按钮之前退出。 要结束按需脚本并让其他脚本在后台运行,您需要另一个解决方案,最多是一个不同的程序,它为您提供一个可以停止它的小界面。或专门结束您的第一个脚本的第二个脚本。

    Set Wshell=CreateObject("Wscript.shell")
    Do
      Wshell.SendKeys "{SCROLLLOCK}"
      continue = MsgBox ("Do you want to press the ScrollLock again?", vbYesNo, "Question")
      Select Case continue
      Case vbNo
        Exit Do
      End Select
      WScript.sleep 10000
    Loop
    

    取自这篇文章:How to stop a vb script running in windows

    Option Explicit
    Dim WshShell
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run "taskkill /f /im Cscript.exe", , True 
    WshShell.Run "taskkill /f /im wscript.exe", , True  
    

    还有第二个脚本可以以艰难的方式杀死您的脚本。我不知道你会如何想象停止脚本?如果没有 GUI,您将无法在您希望的时间执行此操作。要结合解决方案,您可以执行以下操作:

    continue = MsgBox ("Do you want to press the ScrollLock again?", vbYesNo, "Question")
    Select Case continue
    Case vbNo
    Dim WshShell
       Set WshShell = WScript.CreateObject("WScript.Shell")
       WshShell.Run "taskkill /f /im Cscript.exe", , True 
       WshShell.Run "taskkill /f /im wscript.exe", , True  
    End Select
    

    然后将这个弹出的 MsgBox 拉到屏幕的角落,所以如果你不再想要它,只需按“否”就可以了。 ^^

    【讨论】:

    • 我需要运行脚本,直到我选择手动停止它。在时间间隔之后出现的 MsgBox 将删除编写脚本的目的。
    • 添加了几句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2011-08-01
    • 1970-01-01
    相关资源
    最近更新 更多