VBscript automation script stuck when IE modal popup appears - vbscript

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

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

Quit error Internet Explorer Window

Issue : I am currently encountering some issues closing a "premature" internet window which has no content yet.
Basically if this "premature" window is open, my macro doesn't manage to select the window I'm interested in, but as soon as I close this "premature" window manually, I can run my code properly.
Its source comes from a minor error which somehow opens it but except diplaying that window doesn't affect the rest of the code.
Tests done :
Dim Widow As Object, page_foireuse As SHDocVw.InternetExplorer
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
MsgBox objShell.Windows.Count
' MsgBox objShell.Windows(0).document.Title
' MsgBox objShell.Windows(1).document.Title
' MsgBox objShell.Windows(2).document.Title
' MsgBox objShell.Windows(3).document.Title
' MsgBox objShell.Windows(4).document.Title
' MsgBox objShell.Windows(5).document.Title
' MsgBox objShell.Windows(6).document.Title
'For Each Widow In objShell.Windows
' If Widow.document.Title Is Nothing Then ' this doesn't work
' Set page_foireuse = Widow
' End If
'Next
'
'If objShell.Windows(5).document.Title Is Nothing Then
'End If
Set page_foireuse = objShell.Windows(5)
page_foireuse.Quit
MsgBox objShell.Windows.Count
Results so far :
When I count the number of windows in the shell, this "premature" window is also counted
When I return the location or title of each of the counted windows, I get an error when trying to return the location or title of the "premature" window
The two loops I tried to run at this end didn't work
So my question is : How can I close this "premature" window via the macro ?
The page on the image in your question looks strange, no location, no title. Try the following code, but set breakpoint on the line Set doc = ie.document and check if doc is not Nothing etc. HTH
' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library
' Add reference to Microsoft Shell Controls And Automation
Dim ie As SHDocVw.WebBrowser
Dim doc As HTMLDocument
Dim shellApp As Shell32.Shell
Dim windows As SHDocVw.ShellWindows
Dim window
Set shellApp = New Shell
Set windows = shellApp.windows
For Each window In windows
If Not UCase(window.FullName) Like "*IEXPLORE.EXE" Then GoTo continue
Set ie = window
Set doc = ie.document
If doc.Title = "" Then
ie.Quit
Exit For
End If
continue:
Next window

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}"
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

Excel to Web Automation

Here is what I am trying to do via Macro Automation->
-> I am trying to open the website [url]http://www.metaboanalyst.ca/MetaboAnalyst/faces/Home.jsp[/url]
-> Then clicking on the "click here to start"
-> Then checking the radio button for "Peak intensity table"
-> Then click on "Browse" the data file and select a file (File that needs to be selected) manually.
-> Then finally click on the submit button (which is not working and path to the file vanishes while clicking on the submit button and giving me an Automation error ['-2147352319 (80020101)'])
Here is the code that works fine except for the last step which is submitting->
Sub MetaboAnn()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.metaboanalyst.ca/MetaboAnalyst/faces/Home.jsp"
Do Until IE.ReadyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:05"))
'MsgBox "Done"
IE.Visible = True
IE.Document.getElementById("form1:groupPanel1:loginLink").Click
Application.Wait (Now + TimeValue("0:00:05"))
'MsgBox "Done"
IE.Visible = True
IE.Document.getElementById("form1:tabSet1:tab1:layoutPanel7:csvButtonGroup:csvButtonGroup_2_rb").Click
Application.Wait (Now + TimeValue("0:00:03"))
'MsgBox "Done"
IE.Visible = True
IE.Document.all("form1:tabSet1:tab1:layoutPanel7:statCsvUpload_com.sun.webui.jsf.upload").Click
Application.Wait (Now + TimeValue("0:00:03"))
'MsgBox "Done"
IE.Visible = True
IE.Document.getElementById("form1:tabSet1:tab1:layoutPanel7:csvSubmitButton").Click
End Sub
If you are using IE, try to click on TOOLS, then COMPATIBILY VIEW SETTINGS and add the website you're on. It probably has something to do with older code on that website.

Resources