user3727850
user3727850

Reputation: 153

How to click on ok button in message box using vbscript

I have written a simple script to press ok on message box but its not working. Please help me how to do this here is the sample code

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

    oWShell.Sendkeys "{enter}"

Upvotes: 1

Views: 15250

Answers (5)

user21373324
user21373324

Reputation: 1

x=msgbox("Text" ,1, "Title")
if x=1 then (put what you want to do if ok pressed here)
if x=2 then (put what you want to do if cancel pressed here)

Upvotes: 0

hollopost
hollopost

Reputation: 597

how to click on button in message box and control of display time here the script can do that .

just copy this lines of code and paste in text file then save it as "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&""",,""hello"" > 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

Upvotes: 0

Matin Bidgoli
Matin Bidgoli

Reputation: 1

chek this site

you need this:

Set objArgs = WScript.Arguments

messageText = objArgs(0)

MsgBox messageText, 51, "Warning......!"

Upvotes: 0

Bond
Bond

Reputation: 16321

If you just want to close a message box after a certain period of time, check out the Popup() method of the WshShell class. Its second parameter specifies the number of seconds to display the message box for before closing it.

With CreateObject("WScript.Shell")

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

End With

Upvotes: 1

Michael
Michael

Reputation: 2992

MsgBox waits for the click. If you don't click yourself, it gets never to "Sleep" or "SendKeys".

I assume you are just trying to learn, because this code makes no sense. If you want to press a button on another programs window, this could work. But in its own process this doesn't work.

If you really want to click your own MsgBox, you have to do it with a separate script. One creates the MsgBox and another clicks the button.

Upvotes: 1

Related Questions