如何使用vbscript在消息框中单击确定按钮

问题描述 投票:1回答:4

我编写了一个简单的脚本来在消息框中按OK,但是它不起作用。请帮我怎么做这是示例代码

    set oWShell = createobject("WScript.Shell")
    MsgBox "Hello"
    WScript.Sleep 2000

    oWShell.Sendkeys "{enter}"
vbscript
4个回答
0
投票

MsgBox等待点击。如果您不单击自己,则永远不会进入“睡眠”或“发送密钥”。

我假设您只是在尝试学习,因为此代码没有意义。如果要在另一个程序窗口上按一个按钮,则可以使用。但是在它自己的过程中,这是行不通的。

如果您确实要单击自己的MsgBox,则必须使用单独的脚本来完成。一个创建MsgBox,另一个单击该按钮。


0
投票

如果只想在一段时间后关闭消息框,请签出Popup()类的WshShell方法。它的第二个参数指定关闭消息框之前显示消息框的秒数。

With CreateObject("WScript.Shell")

    ' Display a message box that disappears after two seconds...
    .Popup "Hello", 2

End With

0
投票

如何在消息框中单击按钮并在此处控制显示时间,脚本可以做到这一点。

只需复制此行代码并粘贴到文本文件中,然后将其另存为“ ControlMsgBox.vbs”。

   '''                  IN THE NAME OF ALLAH
' THIS SCRIPT CONTROL OF MSGBOX DISPLAY TIME
' LET SENKEYS DEAL WITH THE MSGBOX  
' SOLVE THE PROBLEM OF  APPACTIVATE NOT WORKING EFFECTIVE
On Error Resume Next
Dim Sh : Set Sh=CreateObject("wscript.shell") ' declare and create the wshshell
Dim path : path =Replace(WScript.ScriptFullName,WScript.ScriptName,"") 'declare the variable of the current script path
Dim myMessage : myMessage="This is my message ." 'declare variable of the of the display text of msgbox
Sh.run "cmd.exe /c cd """&path&""" && echo msgbox """&myMessage&""",,""In_The_Name_Of_Allah"" > mymsgbox.vbs",0,false 'create masgbox script in the same path 
WScript.Sleep 1000      'wait 1 sec to let process of create msgbox script execute
Sh.run "mymsgbox.vbs"   'run the created msgbox script
WScript.Sleep 3000      ' let the msgbox display for 3 sec before we sendkeys to close
For i=0 To 600         ' loop to retry select correct msgbox window about 1 min
    ret = Sh.AppActivate("In_The_Name_Of_Allah") 'select the activate msgbox window (if this loop 300 this mean loop will continue 30 sec if 600 (1 min)
    If ret = True Then  ' check if the msgbox windows select or not 
       Sh.SendKeys "%N"   'send key of Alt+N to select first button in msgbox (ok)   
    End If 

    ret = Sh.AppActivate("In_The_Name_Of_Allah")  'recheck again to be sure that we will not send key out of target windows (msgbox window)
    If ret = True Then 
        Sh.SendKeys "{enter}"    ' send key to click enter 
        wscript.sleep 500
        ret=Sh.AppActivate("In_The_Name_Of_Allah")
        If ret=False Then ' using nested IF to sure of selected windows is false because its close 
           Exit For        ' exit for loop directly and execute what after  for next
        End If 
    End If 

    WScript.Sleep 100

    Next
   With CreateObject("Scripting.FileSystemObject")
   If .FileExists(path&"mymsgbox.vbs") Then   'check if the msgbox script we create form this script exist or not
      .DeleteFile(path&"mymsgbox.vbs")        'delete the msgbox script we create after message window closed
   End If 
   End With
    Set Sh=Nothing 'remove the sh object create 
    WScript.Quit   ' terminate wscript.exe instance who run this script

0
投票

检查此site

您需要这个:

设置objArgs = WScript.Arguments

messageText = objArgs(0)

MsgBox messageText,51,“警告......!”

© www.soinside.com 2019 - 2024. All rights reserved.