VBScript Scripting Techniques > User Interaction > User Input
| WSH (StdIn or InputBox) | |
|---|---|
| VBScript Code: | |
|
strInput = UserInput( "Enter some input:" ) WScript.Echo "You entered: " & strInput Function UserInput( myPrompt ) ' This function prompts the user for some input. ' When the script runs in CSCRIPT.EXE, StdIn is used, ' otherwise the VBScript InputBox( ) function is used. ' myPrompt is the the text used to prompt the user for input. ' The function returns the input typed either on StdIn or in InputBox( ). ' Written by Rob van der Woude ' http://www.robvanderwoude.com ' Check if the script runs in CSCRIPT.EXE If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then ' If so, use StdIn and StdOut WScript.StdOut.Write myPrompt & " " UserInput = WScript.StdIn.ReadLine Else ' If not, use InputBox( ) UserInput = InputBox( myPrompt ) End If End Function |
|
| Requirements: | |
| Windows version: | All |
| Network: | N/A |
| Client software: | Windows Script 5.6 for Windows 98, ME, and NT 4 |
| Script Engine: | WSH |
| Summarized: | Works in any Windows version, provided Windows Script 5.6 is installed for Windows 98, ME, and NT 4. |
| [Back to the top of this page] | |
| InternetExplorer.Application | |
| VBScript Code: | |
|
strUserInput = GetUserInput( "Please enter your name:" ) WScript.Echo "Your name is: " & strUserInput Function GetUserInput( myPrompt ) ' This function uses Internet Explorer to ' create a dialog and prompt for user input. ' ' Argument: [string] prompt text, e.g. "Please enter your name:" ' Returns: [string] the user input typed in the dialog screen ' ' Written by Rob van der Woude ' http://www.robvanderwoude.com Dim objIE ' Create an IE object Set objIE = CreateObject( "InternetExplorer.Application" ) ' specify some of the IE window's settings objIE.Navigate "about:blank" objIE.Document.Title = "Input required" objIE.ToolBar = False objIE.Resizable = False objIE.StatusBar = False objIE.Width = 320 objIE.Height = 180 ' 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""><P>" & myPrompt _ & "</P>" & vbCrLf _ & "<P><INPUT TYPE=""text"" SIZE=""20"" " _ & "ID=""UserInput""></P>" & vbCrLf _ & "<P><INPUT TYPE=""hidden"" ID=""OK"" " _ & "NAME=""OK"" VALUE=""0"">" _ & "<INPUT TYPE=""submit"" VALUE="" OK "" " _ & "OnClick=""VBScript:OK.Value=1""></P></DIV>" ' Make the window visible objIE.Visible = True ' Wait till the OK button has been clicked Do While objIE.Document.All.OK.Value = 0 WScript.Sleep 200 Loop ' Read the user input from the dialog window GetUserInput = objIE.Document.All.UserInput.Value ' Close and release the object objIE.Quit Set objIE = Nothing End Function |
|
![]() |
|
| 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] | |