Welcome !
         

 Detect if user "x's" out of window

Author Message
margentieri

  • Total Posts : 18
  • Scores: 0
  • Reward points : 0
  • Joined: 9/19/2016
  • Status: offline
Detect if user "x's" out of window Friday, October 28, 2016 7:08 AM (permalink)
0
Hi all,
 
I have a VBScript which creates a user form within an Internet Explorer window.  This window is created using CreateObject(InternetExplorer.Application).  The problem is, if the user closes this window by x'ing out, the window will close, but the script will then throw an error.  I want to avoid getting this error.
 
I know that one way to do this is to use wscript.CreateObject(InternetExplorer.Application, "IE_") and then use
Sub IE_onQuit
    wscript.quit
End Sub
 
However, this will not work for me, as the engine used by the device I need to run this code on does not recognize the wscript object (crazy I know!).
 
I also tried using On Error Resume Next, but that also doesn't work, because that causes my script to get caught in an infinite loop and never close unless I kill the process in the Task Manager.
 
I figure if I can have some code that detects if the user x's out, I can then simply execute IE.Quit, which would prevent any errors from arising. Does anyone have any suggestions?
 
Thanks!
<message edited by margentieri on Friday, October 28, 2016 7:10 AM>
 
#1
    59cobalt

    • Total Posts : 2625
    • Scores: 253
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Detect if user "x's" out of window Saturday, October 29, 2016 10:35 AM (permalink)
    0
    Not every engine capable of running VBScript code provides a WScript object (the HTA interpreter for instance). Nothing crazy about it.

    The cleanest way is to find out what "exit self" method the interpreter you're using provides, and use that instead of WScript.Quit. If that's not possible for some reason you could resort to checking if your IE handle is still valid:
    Function IsObjectValid(ie)
      IsObjectValid = (TypeName(ie) = "IWebBrowser2")
    End Function
    and run whatever code you want to execute only if that function returns True.

    Also, there's always the ugly On Error Resume Next option, if all else fails.
     
    #2
      margentieri

      • Total Posts : 18
      • Scores: 0
      • Reward points : 0
      • Joined: 9/19/2016
      • Status: offline
      Re:Detect if user "x's" out of window Monday, October 31, 2016 6:14 AM (permalink)
      0
      Unfortunately, there is zero documentation regarding VBScripting for the machine I am using, so I have no idea what the "exit self" method of the interpreter used by this machine is.  The machine is a robotic device that is programmed it's own code language (which has limited functionality), but that language has one function of which can call and execute a VBScript, which is what I have been doing.
       
      I tried your Function solution, but unfortunately I can't get it to work consistently. It wouldn't work at all until I added a sleep at the top of my code loop, and now with the sleep, it seems like it only works if I close out while it is sleeping.  Otherwise, it will throw me an error on whatever line of the loop it is on since the IE object is no longer valid. I thought of using the function to end the script instead of allowing it to continue, but since I can't use wscript.quit, and since the IE object won't be valid anymore at that point, preventing me from using IE.quit, I don't know how to go about quitting the script.
       
      Unfortunately, as I previously stated, I cannot use On Error Resume Next, as this causes my code to enter into an infinite loop.
       
      Any other suggestions? If I could capture the x-out button click event somehow, then I could use IE.quit before the IE window actually closes.  Otherwise, I was thinking of just removing the system menu bar completely, so that there is no x for the user to x-out with, but I haven't been able to find a way to do that.
       
      Any help would be greatly appreciated!
       
      #3
        59cobalt

        • Total Posts : 2625
        • Scores: 253
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:Detect if user "x's" out of window Monday, October 31, 2016 7:53 AM (permalink)
        0
        Sorry, but people here aren't psychic, and our crystal balls are at the cleaner's. Without documentation or at least knowing what kind of device (and interpreter) you're dealing with there's nothing we could do to help you.
         
        #4
          omegastripes

          • Total Posts : 198
          • Scores: 73
          • Reward points : 0
          • Joined: 2/4/2013
          • Status: offline
          Re:Detect if user "x's" out of window Tuesday, November 01, 2016 7:05 AM (permalink)
          0
          margentieri
          The problem is, if the user closes this window by x'ing out, the window will close, but the script will then throw an error.
          Please post the code you are running, and point which is the error line.
          margentieri
          I also tried using On Error Resume Next, but that also doesn't work, because that causes my script to get caught in an infinite loop...
          Please post the snippet with OERN too.
          margentieri
          ... I can then simply execute IE.Quit, which would prevent any errors from arising. ... 
          Could you please elaborate how IE.Quit would help you?
          margentieri
          ... so I have no idea what the "exit self" method of the interpreter used by this machine is.
          I guess the only way is to execute last line of code, skipping unwanted part with Do <condition> ... Loop and If <condition> Then.
          margentieri
          ... until I added a sleep at the top of my code loop, and now with the sleep, it seems like it only works if I close out while it is sleeping.
          How did you implement sleep not having available Wscript object?
          margentieri
           If I could capture the x-out button click event somehow, then I could use IE.quit before the IE window actually closes.
          It's not quite clear how does it helps you, any code snippet?
           
          Why not to use HTA to create the user form? Also this answer http://www.visualbasicscript.com/fb.ashx?m=106025 may help (just remove On Error Resume Next).
           
          Try the below workarounds allowing to detect IE window closing:
           
          1st option
            
           
          Set oWshExec = CreateObject("WScript.Shell").Exec("C:\Program Files\Internet Explorer\iexplore.exe ""about:blank""")
           Do
               For Each oShellWnd In CreateObject("Shell.Application").Windows
                   If oShellWnd.LocationName = "about:blank" Then Exit Do
               Next
               If oWshExec.Status <> 0 Then
                   Set oShellWnd = Nothing
                   Exit Do
               End If
           Loop
           If oShellWnd Is Nothing Then
               MsgBox "Fail to open IE"
           Else
               Do
                   Sleep 1
               Loop Until oWshExec.Status <> 0
               MsgBox "IE window closed"
           End If
           Sub Sleep(lngDelay)
               CreateObject("WScript.Shell").Run "Timeout /T " & lngDelay & " /nobreak", 0, True
           End Sub
            
           

           
          2nd option
            t = 1 / (24 * 60 * 60) ' 1 sec
           Set oIE = CreateObject("InternetExplorer.Application")
           oIE.Visible = True
           oIE.Navigate "about:blank"
           Set d = CreateObject("Scripting.Dictionary")
           Set oWnd = oIE.Document.parentWindow
           oWnd.execScript "var d;"
           Set oWnd.d = d
           d(1) = Now
           oWnd.execScript "d(0) = d(1); setInterval(""d(0) = d(1); write(d(1));"", 500);"
           Do
               Sleep 1
               d(1) = Now
               Sleep 1
           Loop Until d(1) - d(0) > t
           MsgBox "IE window closed"
           Sub Sleep(lngDelay)
               CreateObject("WScript.Shell").Run "Timeout /T " & lngDelay & " /nobreak", 0, True
           End Sub
           
           

           
          Timeout command used to emulate sleep there. Check if your device VBS interpreter allows to run other processes like cmd.exe or wmic.exe.
           
           
           
          #5
            margentieri

            • Total Posts : 18
            • Scores: 0
            • Reward points : 0
            • Joined: 9/19/2016
            • Status: offline
            Re:Detect if user "x's" out of window Wednesday, November 02, 2016 3:33 AM (permalink)
            0
            My original solution was actually an HTA window called though VBScript, but that caused me all kinds of issues.
             
            The device I am working with is a Tecan Freedom EVOware.
             
            IE.Quit would help if I can get it to execute before the IE object is destroyed by closing the application without any errors. Currently, trying to close the app by x'ing out throws all sorts of errors.  I can't be more specific, as the error is different nearly every time. It appears that the error produced is related to whatever line of code the program is on within the loop when I try to x out.  These errors are produced on a normal windows machine (i.e. not just the Tecan), whenever I attempt to close the program by x'ing out.  The only way I have been able to stop those errors is by using wscript.CreateObject(InternetExplorer.Application, "IE_") and then use
            Sub IE_onQuit
                wscript.quit
            End Sub
            however, as I previously stated, while this solution works great on a windows machine, it is not a solution on the Tecan as the Tecan does not recognize the wscript object.
             
            I just want to find a different way of achieving the same result as the IE_onQuit sub (i.e. being able to x out without throwing any errors) that does not invoke wscript.
             
            Here is my code:
             sProtocolMessage = "<font color=black><b>Please select the Protocol you wish to run</b></font>"
            sProtocolListBox = "<select name=ProtocolListBox id=ProtocolListBox size=3>" _
                & "<option value='Diulent Top Off'>Diulent Top Off</option>" _
                & "<option value='Create Baseline Plate'>Create Baseline Plate</option>" _
                & "<option value='Create Range C Mother Plate'>Create Range C Mother Plate</option>" _
                & "</select>"
            '''''
            sPlateNumberMessage = "<font color=black><b>Please select the Number of Plates you wish to run</b>"
            sPlateListBox = "<select name=PlateListBox id=PlateListBox size=9>" _
                & "<option value=1>1</option>" _
                & "<option value=2>2</option>" _
                & "<option value=3>3</option>" _
                & "<option value=4>4</option>" _
                & "<option value=5>5</option>" _
                & "<option value=6>6</option>" _
                & "<option value=7>7</option>" _
                & "<option value=8>8</option>" _
                & "<option value=9>9</option>" _
                & "</select>"
            '''''
            sVolumeMessage = "<font color=black><b>Please enter the Volume in uL you wish to dispense</b><br>" _
                & "The standard dispense volumes are as follows:<br>" _
                & "Diulent Top Off - 54<br>" _
                & "Baseline Plate - 55<br>" _
                & "Range C Mother Plate - 30<br>" _
                & "</font>"
            
            sVolumeRadioButtonStandard = "<input type=radio name=VolumeRadio id=StandardRadio value=Standard checked> Use Standard Volumes"
            sVolumeRadioButtonCustom = "<input type=radio name=VolumeRadio id=CustomRadio value=Custom> Custom"
            sVolumeInputBox = "<input type=text name=VolumeInputBox id=VolumeInputBox>"
            '''''
            sSubmit = "<input type=submit name=Submit id=Submit value=OK style=width:70px onclick='done.value=""clicked""'>"
            sHidden = "<input type=hidden id=done value=false>"
            '''''
            sHTMLCode = sProtocolMessage & "<br>" & "<br>" _
                & sProtocolListBox & "<br>" & "<br>" & "<hr>" _
                & sPlateNumberMessage & "<br>" & "<br>" _
                & sPlateListBox & "<br>" & "<br>" & "<hr>" _
                & sVolumeMessage & "<br>" _
                & "<div style=text-align:left;>" & sVolumeRadioButtonStandard & "<br>" _
                & sVolumeRadioButtonCustom & "&nbsp;" & "&nbsp;" & "&nbsp;" & "&nbsp;" & "&nbsp;" & "&nbsp;" & "&nbsp;" & sVolumeInputBox & "</div>" & "<br>" & "<hr>" & "<br>" _
                & sSubmit _
                & sHidden
            '''''
            Const READYSTATE_UNINITIALIZED = 0, READYSTATE_LOADING = 1, READYSTATE_LOADED = 2, READYSTATE_INTERACTIVE = 3, READYSTATE_COMPLETE = 4
            '''''
            Set IE = CreateObject("InternetExplorer.Application")
            With IE
                .Navigate "about:blank"
                While .ReadyState <> READYSTATE_COMPLETE
                    Sleep 0.01
                Wend
                .ToolBar    = False
                .StatusBar  = False
                .Resizable  = False
                .Height     = 620
                .Width      = 400
                .Left        = 600
                .Top         = 200
                .document.title = "Artel Protocol Options"
                .document.body.style.backgroundcolor = "lightgrey"
                .document.body.innerHTML = "<center>" & sHTMLCode & "</center>"
                .Visible = True
                Do Until Protocol_Type <> "" And Number_of_Plates <> "" And Dispense_Volume <> ""
                    If .document.getElementById("StandardRadio").checked = True Then
                        .document.getElementById("VolumeInputBox").disabled = True
                    ElseIf .document.getElementById("CustomRadio").checked = True Then
                        .document.getElementById("VolumeInputBox").disabled = False
                    End If
                    
                    If .document.all.done.value = "clicked" Then
                        .document.all.done.value = False
                        sProtocol_Type = .document.getElementById("ProtocolListBox").value
                        sNumber_of_Plates = .document.getElementById("PlateListBox").value
                        
                        If .document.getElementById("StandardRadio").checked = True Then
                            If sProtocol_Type = "Diulent Top Off" Then
                                sDispense_Volume = 54
                            ElseIf sProtocol_Type = "Create Baseline Plate" Then
                                sDispense_Volume = 55
                            ElseIf sProtocol_Type = "Create Range C Mother Plate" Then
                                sDispense_Volume = 30
                            End If
                        Else sDispense_Volume = .document.getElementById("VolumeInputBox").value
                        End If
                        
                        If sProtocol_Type = "" Or sNumber_of_Plates = "" Or sDispense_Volume = "" Then
                            MsgBox "All fields must be completed! Please enter all values before submitting.", vbCritical
                        ElseIf IsNumeric(sDispense_Volume) = False Then
                            MsgBox "Invalid Dispense Volume! Acceptable values must be numberic, no less than 10, and no greater than 150. " _
                            & "Please enter a valid Dispense Volume.", vbCritical
                        ElseIf sDispense_Volume < 10 Or sDispense_Volume > 150 Then
                            MsgBox "Invalid Dispense Volume! Acceptable values must be numberic, no less than 10, and no greater than 150. " _
                            & "Please enter a valid Dispense Volume.", vbCritical
                        ElseIf MsgBox("You have selected the " & Chr(34) & sProtocol_Type & Chr(34) & " Protocol for " _
                            & sNumber_of_Plates & " Plates, with a Dispense Volume of " _
                            & sDispense_Volume & ".  Is this correct?", vbInformation + vbYesNo) = vbYes Then
                            
                            Protocol_Type = sProtocol_Type
                            Evoware.SetStringVariable "Protocol_Type",Protocol_Type
                            
                            Number_of_Plates = sNumber_of_Plates
                            Evoware.SetDoubleVariable "Number_of_Plates",Number_of_Plates
                            
                            Dispense_Volume = sDispense_Volume
                            Evoware.SetDoubleVariable "Dispense_Volume",Dispense_Volume
                            
                        Else MsgBox "Please re-enter your protocol parameters.", vbInformation
                        End If
                    End If
                Loop
                .document.all.done.value = True
                .Quit
            End With
            
            Sub Sleep(strTimeWait)
            CreateObject("WScript.Shell").Run "%comspec% /c ping.exe 127.0.0.1 -w 1000 -n " _
                & strTimeWait + 1, 0, True
            End Sub
             

             
            #6
              omegastripes

              • Total Posts : 198
              • Scores: 73
              • Reward points : 0
              • Joined: 2/4/2013
              • Status: offline
              Re:Detect if user "x's" out of window Thursday, November 03, 2016 11:55 AM (permalink)
              0
              I want to suggest you alternative method - to put all control logic into IE page. Try to execute the below code to find out if such approach possible or not:
               
                
               
                  Set oIE = CreateObject("InternetExplorer.Application")
                   With oIE
                       .Navigate "about:blank"
                       Do While .ReadyState <> 4
                           Sleep 1
                       Loop
                       .Visible = True
                       .document.write "<html><head><script>function setParams() {Evoware.SetStringVariable(""Protocol_Type"", ""Diulent Top Off"");Evoware.SetDoubleVariable(""Number_of_Plates"", ""1"");Evoware.SetDoubleVariable(""Dispense_Volume"", ""54"");}</script></head><body><button onclick=""setParams();"">Set Params</button></body></html>"
                       Set oWnd = oIE.Document.parentWindow
                       oWnd.execScript "var Evoware;"
                       Set oWnd.Evoware = Evoware
                       
                   End With
                   MsgBox "Press button in IE window"
                   
                   Sub Sleep(strTimeWait)
                       CreateObject("WScript.Shell").Run "%comspec% /c ping.exe 127.0.0.1 -w 1000 -n " & strTimeWait, 0, True
                   End Sub
                
               

               
              Run the script, wait until IE appears, press button "Set Params", and check if actual parameters have been changed in the Tecan.
               
              #7
                margentieri

                • Total Posts : 18
                • Scores: 0
                • Reward points : 0
                • Joined: 9/19/2016
                • Status: offline
                Re:Detect if user "x's" out of window Friday, November 04, 2016 2:31 AM (permalink)
                0
                Thanks for your help.  I tried running your script, but it gives me the error, "Class does not support automation: oIE.Document.parentWindow".
                 
                #8
                  margentieri

                  • Total Posts : 18
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 9/19/2016
                  • Status: offline
                  Re:Detect if user "x's" out of window Friday, November 04, 2016 4:33 AM (permalink)
                  0
                  Since I don't seem to be getting anywhere with this, I'm thinking it might be best to go another route.  Another possibility could be to hide the system menu entirely, so that the x-close, minimize, and maximize buttons are not available, and then create an Exit button within my script.  I have tried finding a way to hide the system menu within an IE window, but haven't had much success thus far.  Is that something you might know how to accomplish?
                   
                  I really want to thank everyone for their assistance.  I am very new to VB, and in general somewhat of a novice coder. All the help I've been able to get online has been wonderful. Thanks guys.
                   
                  #9
                    omegastripes

                    • Total Posts : 198
                    • Scores: 73
                    • Reward points : 0
                    • Joined: 2/4/2013
                    • Status: offline
                    Re:Detect if user "x's" out of window Friday, November 04, 2016 5:47 AM (permalink)
                    0
                    margentieri
                     Another possibility could be to hide the system menu entirely, so that the x-close, minimize, and maximize buttons are not available...
                    Even if you hide IE window controls, pressing Alt+F4 will allow user to close the window.
                     
                    Since you can create IE instance, I guess your machine runs on Windows, thus it has file system. So you can try to create .vbs file, write some code to it - something simple to test, like MsgBox TypeName(Me), and launch it using the below VBS syntax:
                    CreateObject("WScript.Shell").Run "wscript.exe ""test.vbs"""
                    where test.vbs should be the full path to file you created, maybe it's necessary to put full path to wscript.exe also, it depends on the OS setup. Once the script is launched with standard wscript.exe, there will be no issues with On Error Resume Next processing, WScript object will be available, and stuff like oIE.Document.parentWindow, oIE.GetProperty, oIE.PutProperty will work.
                    CreateObject("WScript.Shell").Run
                     
                    #10
                      margentieri

                      • Total Posts : 18
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 9/19/2016
                      • Status: offline
                      Re:Detect if user "x's" out of window Monday, November 07, 2016 7:29 AM (permalink)
                      0
                      Great suggestion.  Unfortunately, that solution doesn't work either.  The Tecan waits for the VBScript it is directed at to complete before continuing, which in this case is the run script, not the actual script, so unless I use the bWaitonReturn argument, the Tecan script will continue before I have the chance to use my GUI script to pass arguments to it, and when I use that argument, it causes the very problem we have been trying to solve regarding receiving an error on closing the IE window.
                       
                      I also had previously tried using OpenTextFile().ReadAll(), but that also presents the same issue.
                       
                      You are obviously correct in that there are other ways to close the IE window other than x'ing out, such as Alt+F4, but as this issue isn't going away very easily, I still think this would be the best route to go.  Sure, the user could close the program in that fashion and throw an error, but I think they would be much more likely to try to x-out, and if they can't do that, then click on a close button within the GUI. I certainly know that's what I would do as an end user. Unless you have some more suggestions, do you know a way by which I could disable the window controls for my IE window? I've been looking around and still coming up blank.
                       
                      Thanks again!
                       
                       
                       
                      #11
                        omegastripes

                        • Total Posts : 198
                        • Scores: 73
                        • Reward points : 0
                        • Joined: 2/4/2013
                        • Status: offline
                        Re:Detect if user "x's" out of window Wednesday, November 09, 2016 12:06 AM (permalink)
                        0
                        margentieri
                        ... Unless you have some more suggestions, do you know a way by which I could disable the window controls for my IE window?
                        What you are looking for is switching IE to full screen mode:
                          
                        With CreateObject("InternetExplorer.Application")
                             .Navigate "about:blank"
                             Do While .Busy Or .ReadyState <> 4
                                 WScript.Sleep 100
                             Loop
                             .Document.write "FULLSCREEN MODE"
                             .Document.body.style.background = "buttonface"
                             .Fullscreen = True
                             .Visible = True
                         End With
                         
                         

                        Although, that’s just workaround IMO, and it isn’t right that elegant solution has not been found yet.
                        margentieri
                        ... and when I use that argument, it causes the very problem we have been trying to solve regarding receiving an error on closing the IE window. ...
                        Sorry, seems I'm missing something... I saw you implemented Sleep function using ping.exe in the above code, and bWaitonReturn argument is set to True there, is script waiting for the operation ends? If so, then you would be able to "pause" the Tecan's script, while the target saved to file script is running. Thus the last one would have WScript object available, and you could do all stuff I described above in the previous post.
                         
                         
                        #12
                          margentieri

                          • Total Posts : 18
                          • Scores: 0
                          • Reward points : 0
                          • Joined: 9/19/2016
                          • Status: offline
                          Re:Detect if user "x's" out of window Thursday, November 10, 2016 2:18 AM (permalink)
                          0
                          I was trying to avoid having to go full screen. I was hoping I could remove those buttons without having to change the size of my IE window. I'm guessing that's not possible?
                           
                          I'm not sure what you are asking. My sleep sub is only waiting for the amount of sleep time I specify.
                          As for the "run" script solution, that also presents another problem, as Windows does not recognize the Evoware.SetStringVariable and Evoware.SetDoubleVariable objects.  This would require I have at least these lines of code executed within the script executed by the Tecan. I could do that via OpenTextFile().ReadAll(), but as I previously stated, that seems to be causing the same issue.
                           
                          #13
                            margentieri

                            • Total Posts : 18
                            • Scores: 0
                            • Reward points : 0
                            • Joined: 9/19/2016
                            • Status: offline
                            Re:Detect if user "x's" out of window Thursday, December 22, 2016 4:40 AM (permalink)
                            0
                            I found my solution. I added an On Error Resume Next statement, and then updated the line
                             If sProtocol_Type = "" Or sNumber_of_Plates = "" Or sDispense_Volume = "" Then
                                    MsgBox "All fields must be completed! Please enter all values before submitting.", vbCritical
                             ElseIf ....
                             

                             
                            to
                             
                             If sProtocol_Type = "" Or sNumber_of_Plates = "" Or sDispense_Volume = "" Then
                                If Err.Number <> 0 Then
                                    Exit Do
                                Else: MsgBox "All fields must be completed! Please enter all values before submitting.", vbCritical
                                End If
                            ElseIf ....
                             

                             
                            This enabled me to escape the MsgBox that would infinitely repeat when utilizing On Error Resume Next
                             
                            #14
                              Online Bookmarks Sharing: Share/Bookmark

                              Jump to:

                              Current active users

                              There are 0 members and 2 guests.

                              Icon Legend and Permission

                              • New Messages
                              • No New Messages
                              • Hot Topic w/ New Messages
                              • Hot Topic w/o New Messages
                              • Locked w/ New Messages
                              • Locked w/o New Messages
                              • Read Message
                              • Post New Thread
                              • Reply to message
                              • Post New Poll
                              • Submit Vote
                              • Post reward post
                              • Delete my own posts
                              • Delete my own threads
                              • Rate post

                              2000-2017 ASPPlayground.NET Forum Version 3.9