Fileaze banner

 

VBScript Scripting Techniques > User Interaction > Buttons

Buttons Dialog

by Denis St-Pierre

 

InternetExplorer.Application
VBScript Code:
Function IEButtons( )
    ' This function uses Internet Explorer to create a dialog.
    Dim objIE, sTitle, iErrorNum

    ' Create an IE object
    Set objIE = CreateObject( "InternetExplorer.Application" )
    ' specify some of the IE window's settings
    objIE.Navigate "about:blank"
    sTitle="Make your choice " & String( 80, "." ) 'Note: the String( 80,".") is to push "Internet Explorer" string off the window
    objIE.Document.Title = sTitle
    objIE.MenuBar        = False
    objIE.ToolBar        = False
    objIE.AddressBar     = false
    objIE.Resizable      = False
    objIE.StatusBar      = False
    objIE.Width          = 250
    objIE.Height         = 280
    ' Center the dialog window on the screen
    With objIE.Document.ParentWindow.Screen
        objIE.Left = (.AvailWidth  - objIE.Width ) \ 2
        objIE.Top  = (.Availheight - objIE.Height) \ 2
    End With
    ' Wait till IE is ready
    Do While objIE.Busy
        WScript.Sleep 200
    Loop
    

    ' Insert the HTML code to prompt for user input
    objIE.Document.Body.InnerHTML = "<div align=""center"">" & vbcrlf _
                                  & "<p><input type=""hidden"" id=""OK"" name=""OK"" value=""0"">" _
                                  & "<input type=""submit"" value=""  Limited User   "" onClick=""VBScript:OK.Value=1""></p>" _
                                  & "<input type=""submit"" value=""  Standard User "" onClick=""VBScript:OK.Value=2""></p>" _
                                  & "<input type=""submit"" value=""   Power User   "" onClick=""VBScript:OK.Value=4""></p>" _
                                  & "<input type=""submit"" value=""   Admin User   "" onClick=""VBScript:OK.Value=8""></p>" _
                                  & "<p><input type=""hidden"" id=""Cancel"" name=""Cancel"" value=""0"">" _
                                  & "<input type=""submit"" id=""CancelButton"" value=""       Cancel       "" onClick=""VBScript:Cancel.Value=-1""></p></div>"

    ' Hide the scrollbars
    objIE.Document.Body.Style.overflow = "auto"
    ' Make the window visible
    objIE.Visible = True
    ' Set focus on Cancel button
    objIE.Document.All.CancelButton.Focus


    'CAVEAT: If user click red X to close IE window instead of click cancel, an error will occur.
    '        Error trapping Is Not doable For some reason
    On Error Resume Next
    Do While objIE.Document.All.OK.Value = 0 and objIE.Document.All.Cancel.Value = 0
        WScript.Sleep 200
        iErrorNum=Err.Number
        If iErrorNum <> 0 Then    'user clicked red X (or alt-F4) to close IE window
            IEButtons = 0
            objIE.Quit
            Set objIE = Nothing
            Exit Function
        End if
    Loop
    On Error Goto 0

    objIE.Visible = False

    ' Read the user input from the dialog window
    IEButtons = objIE.Document.All.OK.Value
    ' Close and release the object
    objIE.Quit
    Set objIE = Nothing
End Function
 
Sample VBScript Code:
' Save this code as IEButtonsDemo.vbs
WScript.Echo IEButtons( )
 
Sample Batch Code:
FOR /F %%A IN ('CSCRIPT //NoLogo IEButtonsDemo.vbs') DO SET ButtonChoice=%%~A
 
Internet Explorer buttons dialog
 
Requirements:
Windows version: any
Network: any
Client software: Internet Explorer 4 or later
Script Engine: WSH (CSCRIPT and WSCRIPT)
(to use in HTAs, remove both WScript.Sleep lines)
Summarized: Works in all Windows versions with Internet Explorer 4 or later, remove both WScript.Sleep lines to use in HTAs.
 
[Back to the top of this page]

 

 


page last uploaded: 4 March 2011, 12:56