Quit error Internet Explorer Window - windows

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

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.

Access VBA to Close a Chrome window opened via Shell

I am attempting to close a shell Chrome window via a VBA function. My function runs a URL query that returns a .csv file. The thing is I would like to close the window so that it is not always showing (This process runs every 3 minutes). I haven't been able to find a solution that I can get to work as of yet. I tried adding SendKeys "%{F4}" after as one site suggested. This merely minimizes the window, not close it. I also attempted to try adding DoCmd.Close Shell, "Untitled" after, yet this also did not work. I have spent several hours attempting to do, what I imagine is a simple task, and felt another set of eyes could point me in the right direction. Below is my code that opens Chrome. Any assistance is greatly appreciated.
Public Function RunYahooAPI()
Dim chromePath As String
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url http://download.finance.yahoo.com/d/quotes.csv?s=CVX%2CXOM%2CHP%2CSLB%2CPBA%2CATR%2CECL%2CNVZMY%2CMON&f=nsl1op&e=.csv")
End Function
this VBA code will launch (as in your question) chrome, save the Process handle in the variable pHandle, loop all processes with this Handle and then stop the process (after checking user and domain of the process owner) .
Sub LaunchandStopProcess()
'
' As in your Question
'
Dim chromePath As String
Dim pHandle As Variant
chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
'
' Note: Shell pass the Process Handle to the PID variable
'
PHandle = Shell(chromePath & " -url http://download.finance.yahoo.com/d/quotes.csv?s=CVX%2CXOM%2CHP%2CSLB%2CPBA%2CATR%2CECL%2CNVZMY%2CMON&f=nsl1op&e=.csv")
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim ProcToTerminate As String
Dim intError As Integer
Set objWMIcimv2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMIcimv2.ExecQuery("select * from win32_process where Handle='" & CStr(pHandle) & "'")
'
' ObjList contains the list of all process matching the Handle (normally your chrome App, if running)
'
If objList.Count = 0 Then
' No matching Process
' Set all objects to nothing
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
Else
'
' Parse all matching Processes
'
For Each objProcess In objList
' additionally check with actual user
colProperties = objProcess.getowner(strNameofUser, strUserdomain)
If strUserdomain + "\" + strNameofUser = Environ$("userdomain") + "\" + Environ$("username") Then
intError = objProcess.Terminate
If intError <> 0 Then
'
' Trap Error or do nothing if code run unattended
'
Else
' Confirm that process is killed or nothing if code run unattended
End If
End If
Next
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
End If
End Sub

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

Detect when a web page is loaded and IE object gets disconnected, catch disconnection, and then get IE instance again

Detect when a web page is loaded without using sleep
The code below, which came from code in the "UPD2:" section of the above link, does not work. The objIE.getProperty is returning nothing, so it never matches the first tokenized instance strSignature. I was able to isolate that the For Each statement is not locating the open browser instance. Can you please help me fix the code?
I'm on Windows 7 with IE9. My company will soon be upgrading to IE11. I don't think that makes any difference, as I was able to validate that the initial tokenized strSignature is working and being retained. It's just not locating the open browser window and therefore unable to grab the second token for comparison.
For Each objIE In CreateObject("Shell.Application").Windows
' loop through all explorer windows to find tokenized instance
If objIE.GetProperty("marker") = strSignature
As requested here is the full code/paste from the reference link above to make the question more self-contained.
'UPD2: You wrote that IE gets disconnected as the login pop-up comes in, hypothetically there is a way to catch disconnection, and then get IE instance again. Note this is "abnormal programming" :) I hope this helps:
Option Explicit
Dim objIE, strSignature, strInitType
Set objIE = CreateObject("InternetExplorer.Application") ' create IE instance
objIE.Visible = True
strSignature = Left(CreateObject("Scriptlet.TypeLib").GUID, 38) ' generate uid
objIE.putproperty "marker", strSignature ' tokenize the instance
strInitType = TypeName(objIE) ' get typename
objIE.Navigate "https://www.yahoo.com/"
MsgBox "Initial type = " & TypeName(objIE) ' for visualisation
On Error Resume Next
Do While TypeName(objIE) = strInitType ' wait until typename changes (ActveX disconnection), may cause error 800A000E if not within OERN
WScript.Sleep 10
Loop
MsgBox "Changed type = " & TypeName(objIE) ' for visualisation
Set objIE = Nothing ' excessive statement, just for clearance
Do
For Each objIE In CreateObject("Shell.Application").Windows ' loop through all explorer windows to find tokenized instance
If objIE.getproperty("marker") = strSignature Then ' our instance found
If TypeName(objIE) = strInitType Then Exit Do ' may be excessive type check
End If
Next
WScript.Sleep 10
Loop
MsgBox "Found type = " & TypeName(objIE) ' for visualisation
On Error GoTo 0
Do While objIE.ReadyState <> 4 ' conventional wait if instance not ready
WScript.Sleep 10
Loop
MsgBox "Title = " & objIE.Document.Title ' for visualisation

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

Resources