Announcement

Collapse
No announcement yet.

Internet Explorer function

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Internet Explorer function

    Does anyone know how to add a cancel button to quit the entire script into this function? I cannot seem to figure this out...I've tried adding a couple statements to quit the script...but with no luck. Highlighted in blue below is what I have tried:

    Code:
    Function SelectBox(sTitle, aOptions)
       Dim oIE, s, item
       set oIE = CreateObject("InternetExplorer.Application")
          With oIE
             .FullScreen = True
             .ToolBar = False : .RegisterAsDropTarget = False
             .StatusBar = False : .Navigate("about:blank")
    
             Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
             .width= 400 : .height=200
                With .document
                   With .parentWindow.screen
                      oIE.left = (.availWidth - oIE.width ) \ 2
                      oIE.top = (.availheight - oIE.height) \ 2
                   End With
                   s = "<html><head><title>" & sTitle _
                   & "</title></head><script language=vbs>bWait=true<" & "/script>" _
                   & "<body bgColor=Silver><center>" _
                      & "<b>" & sTitle & "<b><p>" _
                   & "<select id=entries size=1 style='width:325px'>" _
                   & " <option selected>" & sTitle & "</option>"
    
                   For each item in aOptions
                      s = s & " <option>" & item & "</option>"
                   Next
    
                   s = s & " </select><p>" _
                   & "<button id=but0 onclick='bWait=false'>OK</button>" _
                   & "<button id=but1 onclick='bWait=true'>CANCEL</button>" _
                   & "</center></body></html>"
                   .open
                   .Write(s)
                   .close
    
    	       If bWait Then WScript.Quit
    		
                   Do until .ReadyState ="complete" : Wscript.Sleep 50 : Loop
                      With .body
                         .scroll="no"
                         .style.borderStyle = "outset"
                         .style.borderWidth = "3px"
                      End With
                   .all.entries.focus
                   oIE.Visible = True
                   CreateObject("Wscript.Shell").AppActivate sTitle
    
                   On Error Resume Next
                   Do While .ParentWindow.bWait
                      WScript.Sleep 100
                      if oIE.Visible Then SelectBox = "Aborted"
                      if Err.Number <> 0 Then Exit Function
                   Loop
                   On Error Goto 0
                      With .ParentWindow.entries
                         SelectBox = .options(.selectedIndex).text
                      End With
                End With
                .Visible = False
          End With
    End Function

  • #2
    Re: Internet Explorer function

    I have no idea what you are up to with IE application object. How about using the MsgBox function and checking its return value?

    -vP

    Comment


    • #3
      Re: Internet Explorer function

      Originally posted by vonPryz View Post
      I have no idea what you are up to with IE application object. How about using the MsgBox function and checking its return value?

      -vP
      Yeah a simple msgbox isn't going to work with what I am doing. The IE application is used to display a list of groups in a drop down box that were found as a result from an AD search. You would then select the group, click "Ok" and continue with the script. My problem is, that there is not a cancel button, or one that I can get working at least

      I don't really think its necessary to post the whole script since this function is the only part dealing with the IE application...but if anyone feels that they need it for reference let me know. I just need a way to cancel out of it...it can't be that hard, I just haven't found a way of doing it.

      Comment


      • #4
        Re: Internet Explorer function

        Does the OK button part work properly? I gather the problem is the IE application won't actually return anything to the calling script, so neither cancel nor OK action would work.

        A solution could be to put the stuff in a HTA and call that part from the calling script via creating a Shell object and executing mshta full-path.hta. The HTA will be able to do lots of things ordinary HTML pages can't.

        -vP

        Comment


        • #5
          Re: Internet Explorer function

          Yeah the OK button works fine...

          An HTA is a solution, but I'd like to try and get this working since I know it has to be a simple fix.

          Comment


          • #6
            Re: Internet Explorer function

            By creating first an input element input name='xxxx' (you can make it type='hidden') you define an object with a value.
            Then you can use an Event attribute (like Onclick) to give it an other value.
            Code:
            <input name='ButtonHandler' type='hidden' value='pre Cancel Value'>
            
            <button id=but1 Onclick="document.all('ButtonHandler').value='CANCELLED'">Cancel</button>
            In this sample the object is named: 'ButtonHandler' - you could give it an other name if you like (the name is case sensitive). In this sample there is only the Cancel button that requires a special handler but normally you would have create this input element probably for multiple Event attributes on the same document.

            next,
            In your sample there is this loop: Do While .ParentWindow.bWait It loops until someone clicks on the OK button or, when the IE window is closed.
            To the function in the sample below two new conditions are added. One extra condition to exit the loop: exit loop when the value of ButtonHandler is changed to "CANCELLED". And one condition to check if a valid option was selected from the list (the invalid option is the value: "Select the Group" (sTitle) which has been added to the top of the list.

            The return value of the vbs function below is "!Aborted" when the Cancel button is clicked, the IE window was Closed, when an error occoured or, when no valid option was selected when the OK button was clicked.
            Else, the return value of the vbs function is the selected option from the Drop Down List.
            Code:
            aOpt = Array("aaa", "bbb", "ccc", "ddd", "eee", "fff")
            
            sGroup = SelectBox("Select the Group", aOpt)
            
            If sGroup = "!Aborted" Then
               wscript.echo "Aborted, script ends here"
               wscript.quit
            End If
            
            wscript.echo "selected item: " & sGroup
            wscript.quit
            
            
            Function SelectBox(sTitle, aOptions)
               Dim oIE, s, item
               SelectBox = "!Aborted"
               set oIE = CreateObject("InternetExplorer.Application")
               Set WSHShell = CreateObject("WScript.Shell")
               With oIE:
                 .FullScreen = FALSE
                 .ToolBar = False : .RegisterAsDropTarget = False
                 .StatusBar = False : .Navigate("about:blank")
                 Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
                 .width= 400 : .height=200
                 With .document
                   .Title = sTitle & String(555, chr(160))
                   with .parentWindow.screen
                     oIE.left = (.availWidth - oIE.width ) \ 2
                     oIE.top = (.availheight - oIE.height) \ 2
                   End With
            
                   s = "<html><head><title>" & sTitle & String(555, chr(160)) _
                     & "</title></head><script language=vbs>bWait=true<" _
                     & "/script><body bgColor=Silver><center>" _
                     & "<b>" & sTitle & "<b><p>" _
                     & "<select id=entries size=1 style='width:325px'>" _
                     & " <option selected>" & sTitle & "</option>"
                   For each item in aOptions
                     s = s & " <option>" & item & "</option>"
                   Next
            
                   .open
                   s = s & " </select><p><input name='ButtonHandler' " _
                     & "type='hidden' value='pre Cancel Value'>" _
                     & "<button id=but0 AccessKey='o' onclick=""bWait=false"">" _
                     & "<u>O</u>K</button> <button name='Cancel'" _
                     & " AccessKey='c' Onclick=""document.all" _
                     & "('ButtonHandler').value='CANCELLED'"">" _
                     & "<u>C</u>ancel</button></center></body></html>"
                   .Write(s)
                   .close
            
                   Do until .ReadyState ="complete" : wsh.Sleep 50 : Loop
                   With .body
                     .scroll="no"
                     .style.borderStyle = "outset"
                     .style.borderWidth = "3px"
                   End With
            
                   .focus(): .all.entries.focus
                   oIE.Visible = True: wsh.Sleep 50
                   WSHShell.SendKeys "%{TAB}"
                   WSHShell.AppActivate ("http:/// - " & sTitle)
            
                   On Error Resume Next: err.clear
                   Do While .ParentWindow.bWait
                     .focus(): .all.entries.focus
                     If oIE.Document.All("ButtonHandler").Value = "CANCELLED" Then
                       oIE.quit: WSHShell.SendKeys "^"
                       Exit Function
                     ElseIf Err.Number <> 0 Then
                       oIE.quit: WSHShell.SendKeys "^"
                       Exit Function
                     End If
                     WScript.Sleep 250
                   Loop
                   On Error Goto 0
                   With .ParentWindow.entries
                     If .options(.selectedIndex).text <> sTitle _
                       Then SelectBox = .options(.selectedIndex).text
                   End With
                 End With
               End With
               oIE.quit: WSHShell.SendKeys "^"
            End Function
            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Internet Explorer function

              Thanks Rems! Works perfectly...as always, thanks for explaining.

              Comment

              Working...
              X