Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create options dialog with VbScript?

Tags:

vbscript

I have a third party application that invokes a vsbscript file for certain operations. I would like to put up a user prompt with a choice of options, either a drop down list or checkbox or some such. However, all I can find is the input box option.

I don't think HTAs are an option in my case (unless there is a way to call them from a .vbs file?)

My other thought was some sort of ActiveX control, but I can't locate a built-in one that would be available by default on WindowsXP/Vista.

Anybody have any ideas on how I could accomplish this?

like image 339
Nathan Avatar asked Dec 08 '22 07:12

Nathan


2 Answers

The simple answer is, you really can't. Tmdean's solution is the only way I can think of either. That said, you can spruce up the input box so it doesn't look horrible. Give this a run, I don't think it's an epic fail:

Dim bullet
Dim response
bullet = Chr(10) & "   " & Chr(149) & " "
Do
    response = InputBox("Please enter the number that corresponds to your selection:" & Chr(10) & bullet & "1.) Apple" & bullet & "2.) Bannana" & bullet & "3.) Pear" & Chr(10), "Select Thing")
    If response = "" Then WScript.Quit  'Detect Cancel
    If IsNumeric(response) Then Exit Do 'Detect value response.
    MsgBox "You must enter a numeric value.", 48, "Invalid Entry"
Loop
MsgBox "The user chose :" & response, 64, "Yay!"
like image 198
Oorang Avatar answered Dec 09 '22 21:12

Oorang


If you would like to use an hta for this it can be done like this.
The VBScript:


Set WshShell = CreateObject("WScript.Shell")
'Run the hta.
WshShell.Run "Test.hta", 1, true
'Display the results.
MsgBox "Return Value = " & getReturn
Set WshShell = Nothing

Function getReturn
'Read the registry entry created by the hta.
On Error Resume Next
     Set WshShell = CreateObject("WScript.Shell")
    getReturn = WshShell.RegRead("HKEY_CURRENT_USER\Volatile Environment\MsgResp")
    If ERR.Number  0 Then
        'If the value does not exist return -1
         getReturn = -1
    Else
        'Otherwise return the value in the registry & delete the temperary entry.
        WshShell.RegDelete "HKEY_CURRENT_USER\Volatile Environment\MsgResp"
    End if
    Set WshShell = Nothing
End Function

Then design the hta as desired, and include the following methods



'Call this when the OK button is clicked.
Sub OK_Click
    For Each objradiobutton In Opt
         If objradiobutton.Checked Then
              WriteResponse objradiobutton.Value
        End If
    Next
    window.Close
End Sub

'Call this when the Cancel button is clicked.
Sub Cancel_Click
     WriteResponse("CANCEL")
     window.Close
End Sub

'Write the response to the registry
Sub WriteResponse(strValue)
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.RegWrite "HKEY_CURRENT_USER\Volatile Environment\MsgResp", strValue
     Set WshShell = Nothing
End Sub

I used a group of radio buttons named "Opt" to make a choice, but you could use any controls you would like.

Because hta's cannot return values, this will create a temperary registry entry. If you are not comforatable messing with the registry, you could also write the result to a temperary text file.

This approach is nice because you can design the hta any way you like, rather than using the supplied inputbox and choosing numbers (thats so DOS).

This could also be nice if you expanded the hta to create itself based on arguments passed to it, like passing in a title, a message to display, an array of options, a set of buttons. That way you could use the same hta any time you needed to get input from the user.

like image 41
Tester101 Avatar answered Dec 09 '22 21:12

Tester101