How to click on ok button in message box using vbscript - 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}"

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.

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

chek this site
you need this:
Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox messageText, 51, "Warning......!"

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

Related

Windows shell script not able to make existing IE window visible?

I have been trying to create a really simple script to maintain visibility on an IE page but am currently unable to force the visibility or foreground position of an existing IE window.
I am able to successfully activate the window using WshShell.AppActivate() but it does not make the page foreground to a user. A sample from my code is below.
Basically the code loops until the user ends the notepad window. my confusion is specifically with why IE is not visible no matter what commands I send
Dim pressX
Dim FindProc
pressX = 1
' Create WScript Shell Object to access filesystem.
Set WshShell = WScript.CreateObject("WScript.Shell")
'WshShell.Run "NoScreenSaver.txt", 2, 0
' Define Which process to seek for to terminate script
strComputer = "."
FindProc = "NoScreenSaver"
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim IE : Set IE = Nothing
Dim objWindow
' Loop start
Do
' Wait for 5 minutes
WScript.Sleep 3000 '000
For Each objWindow In objApp.Windows
If (InStr(objWindow.Name, "Internet Explorer")) Then
Set IE = objWindow
Exit For
End If
Next
' check if specific notepad closed
If WshShell.AppActivate("NoScreenSaver.txt") = True Then
'WshShell.AppActivate("iexplore.exe")
'wscript.echo FindProc & " is running"
Else
wscript.echo FindProc & " is not running" & vbCrLf & "Script will now end"
pressX = 0
End If
IE.visible = True
wshshell.appactivate ie.name
IE.navigate "google.ca"
' Send dummy key
WshShell.SendKeys "{F13}"
' repeat
Loop While pressX
I never wanted to use AppActivate to check for the specific notepad window either since it would take foreground. There is some information regarding using a COM wrapper but the COM wrapper approach is not possible within my constraints.

How to automatically close popup windows by using vbscript?

I want to clear or automatically close popup windows through vbscript, but they may be different popup windows or may be a popup window within popup window.
I am using a vbscript to create pop a window and I want a script which will automatically close all the popup windows. Below is the script which creates pop up windows and I want to close all these pop up windows through script.
Dim WshShell, BtnCode
Set WshShell = WScript.CreateObject("WScript.Shell")
BtnCode = WshShell.Popup("Do you like your job?", 7, "Answer This Question:", 4 + 32)
Select Case BtnCode
case 6 WScript.Echo "That's great!."
case 7 WScript.Echo "Sorry to hear that."
case -1 WScript.Echo "No Response?"
End Select
My solution to above code where it works partially:
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Answer this question")
If ret = True Then
wshShell.SendKeys "%Y"
Exit Do
End If
WScript.Sleep 500
Loop

Is it possible to write a code that opens a program and the login is hidden?

First code: To hide the program when it is running.
Dim WShell
Set WShell = CreateObject("WScript.Shell")
WShell.Run "program name", 0
Set WShell = Nothing
Second code: Runs the program and puts an email and logs in.
Set a = CreateObject("WScript.Shell")
a.Run "program name\"
WScript.Sleep (5000)
a.SendKeys ("email")
a.SendKeys Chr(9)
a.SendKeys "{Enter}"
I am trying to merge the first code with the second code, but I failed
where I want to run the program and login to it via email automatically hidden.
VBScript cannot send keystrokes to hidden windows, meaning what you're asking is not possible in VBScript. It might be possible with AutoIt, though, using the ControlSend method:
Example()
Func Example()
; Run Notepad
Run("notepad.exe")
; Wait 10 seconds for the Notepad window to appear.
Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)
; Wait for 2 seconds.
Sleep(2000)
; Send a string of text to the edit control of Notepad. The handle returned by WinWait is used for the "title" parameter of ControlSend.
ControlSend($hWnd, "", "Edit1", "This is some text")
; Wait for 2 seconds.
Sleep(2000)
; Close the Notepad window using the handle returned by WinWait.
WinClose($hWnd)
; Now a screen will pop up and ask to save the changes, the classname of the window is called
; "#32770" and simulating the "TAB" key to move to the second button in which the "ENTER" is simulated to not "save the file"
WinWaitActive("[CLASS:#32770]")
Sleep(500)
Send("{TAB}{ENTER}")
EndFunc ;==>Example

VBscript automation script stuck when IE modal popup appears

I am using vbscript to automate an website operation using IE.
At one place, clicking a button creates a modal popup. When the popup appears, the script does not execute further and sort of freezes.
I thought of executing a second script, but I am unable to activate the popup window. The popup dialog has title as "Proceed -- Webpage Dialog", and 2 buttons - Ok and Cancel. I'm using the below code to activate the pop up window and then click Ok on it. But I am unable to capture the window. Below is the code for second script.
script2.vbs:
WScript.Sleep 5000
Msgbox "Started 2nd script"
For Each wnd In CreateObject("Shell.Application").Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
If InStr(1, wnd.Document.Title, "Proceed", vbTextCompare) > 0 Then
Set objIE = wnd
MsgBox "Found Window"
Exit For
End If
End If
Next
This does nothing, and I have no clue how to handle this popup. Any thoughts or workarounds for this, will be very helpful.
Update:
Below is the code for the main script.
script1.vbs:
Dim url
url = "http://example.com"
Set objIE = CreateObject("InternetExplorer.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
objIE.Visible = True
objIE.AddressBar = True
objIE.StatusBar = False
objIE.MenuBar = True
WshShell.AppActivate ("Internet Explorer")
objIE.Navigate url
'''
' Some code to perform website operations
'
'
WshShell.Run "script2.vbs"
''The below line triggers the popup
Set btn = objIE.Document.GetElementById("grayBtn").GetElementsByTagName("a").Item(0)
btn.Click

VBScript to Display Countdown using Vbscript Graphical Elements

I wanted to display a MessageBox which displays countdown from 10 to 1 and autocloses after 10 seconds. As Msgbox in vbscript passes code execution untill the user acts on it i tried it using Popup in Wscript Shell Object
Dim counter
Dim oShell
counter = 10
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Left " & counter & " Seconds",1,"Remind"
counter = counter-1
Wend
But it auto-closes for every second and opens a new popup is there any way i can display the countdown and autoclose using the available GUI elements in vb script
Afraid not, the popup is modal & can't be interacted with while its displayed so there is no way to update its existing content.
If you want a more flexible UI you will need to use something different, the console or HTML in an HTA.
You can use Internet Explorer to create a non-modal display.
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.navigate("about:blank")
.Document.Title = "Countdown" & string(100, chrb(160))
.resizable=0
.height=200
.width=100
.menubar=0
.toolbar=0
.statusBar=0
.visible=1
End With
' wait for page to load
Do while oIE.Busy
wscript.sleep 500
Loop
' prepare document body
oIE.document.body.innerHTML = "<div id=""countdown"" style=""font: 36pt sans-serif;text-align:center;""></div>"
' display the countdown
for i=10 to 0 step -1
oIE.document.all.countdown.innerText= i
wscript.sleep 1000
next
oIE.quit
I have a code, but it might not help
dTimer=InputBox("Enter timer interval in minutes","Set Timer") 'minutes
do until IsNumeric(dTimer)=True
dTimer=InputBox("Invalid Entry" & vbnewline & vbnewline & _
"Enter timer interval in minutes","Set Timer") 'minutes
loop
flop=InputBox("What do you want to set the timer for?")
if dTimer<>"" then
do
WScript.Sleep dTimer*60*1000 'convert from minutes to milliseconds
Set Sapi = Wscript.CreateObject("SAPI.spVoice")
Sapi.speak "Time's up."
t=MsgBox(flop & vbnewline & vbnewline & "Restart Timer?", _
vbYesNo, "It's been " & dTimer &" minute(s)")
if t=6 then 'if yes
'continue loop
else 'exit loop
exit do
end if
loop
end if
please excuse my weird variable names.
This code is from https://wellsr.com/vba/2015/vbscript/vbscript-timer-with-wscript-sleep/
It actually is possible to edit the countdown, very easily for VBScript, so I don't understand what the problem is.
Here's the code:
Dim counter
Dim oShell
counter =InputBox("")
#if you add =InputBox you can have the choice to type in the amount of times the popup repeats#
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Time until shutdown " & counter & " Seconds",(1),"[3]Critical Viruses Detected
#and if you change the (1) to whatever number you want then you can change how fast the popup repeats and if you join this with the "open command" then you can make a gnarly nondangerous virus#
counter = counter-1
Wend

Resources