vbscripthtaadsutil.vbs

How to pass HTA Form data (as months and years) to an external Vbscipt file


I'm creating an HTA from which to run some external VBS files. I need to pass from the HTA to the VBS files, the data of some forms (Radio Button). What should I add to the HTA file to accomplish it? And in VBS?

This is the code I have created so far:

<head><title>Dashboard_Script</title></head>
<meta http-equiv="x-ua-compatible" content="IE=10">
<Script language="VBScript">Set Wss=CreateObject("WScript.Shell")</script>

<body><table><tr><td align="center">

<br><br> DASHBOARD SCRIPTS<br>

<form name="Form1">

<input type="Button" name="Button1" value="SalesReport ">

           
<SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript">

Wss.Run("C:\SalesReport.vbs")

</SCRIPT>

<table>

<tr>

<p>Please select Month from:</p>

<th><input type="radio" id="age1" name="age1" value="1">

<label for="age1">1</label><br></th>

<th><input type="radio" id="age1" name="age1" value="2">

<label for="age2">2</label><br></th>

.........................................................
.........................................................

</tr>

</table>

</fieldset>

</form>

Solution

  • Don't use a form. That's for passing data to a server. You can get the value of the selected radio button, but since you must iterate through radio buttons to find out which one is selected, you can just use your loop counter as the month index. Also consider using a Select menu or a series of buttons. The code below shows all three methods. In all cases, it passes the month index on the command line to the VBS script. The code below passes a value of 1-12. However, if the receiving script is set up with an array for the months, it may be more convenient to pass a zero based index (i.e. 0-11). It's a small adjustment.

    This code can be expanded, of course, for additional parameters, such as a year.

    Note that the code is using MsgBox for debugging purposes. Also, avoid using tables. When it comes time to make it pretty, use Divs with classes and CSS for styling.

    <!DOCTYPE html>
    <html>
    <head>
    <title>Dashboard_Script</title>
    <meta charset="UTF-8" http-equiv="x-ua-compatible" content="IE=9">
    <script language="VBScript">
    Set oWSH=CreateObject("WScript.Shell")
    
    Sub SalesReport1
      For i = 0 To 11
        If Age1(i).Checked Then Exit For
      Next
      If i<12 Then
        MsgBox "C:\SalesReport.vbs " & i+1
        'oWSH.Run("C:\SalesReport.vbs " & i+1)
      End If
    End Sub
    
    Sub SalesReport2
      If Age2.SelectedIndex<>0 Then
        MsgBox "C:\SalesReport.vbs " & Age2.SelectedIndex
        'oWSH.Run("C:\SalesReport.vbs " & Age2.SelectedIndex)
      End If
    End Sub
    
    Sub SalesReport3(i)
      MsgBox "C:\SalesReport.vbs " & i
      'oWSH.Run("C:\SalesReport.vbs " & i)
    End Sub
    
    </script>
    </head>
    <body>
    <h2>Dashboard Scripts</h2>
    <input type=button value=SalesReport onclick=SalesReport1()>
    Please select Month:<br>
    <input type=radio name=Age1>January
    <input type=radio name=Age1>February
    <input type=radio name=Age1>March
    <input type=radio name=Age1>April
    <input type=radio name=Age1>May
    <input type=radio name=Age1>June
    <input type=radio name=Age1>July
    <input type=radio name=Age1>August
    <input type=radio name=Age1>September
    <input type=radio name=Age1>October
    <input type=radio name=Age1>November
    <input type=radio name=Age1>December
    <br><br>
    
    <input type=button value=SalesReport onclick=SalesReport2()>
    Please select:
    <select id=Age2>
    <option selected disabled>Month</option>
    <option>January</option>
    <option>February</option>
    <option>March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November</option>
    <option>December
    </select><br><br>
    
    Click a sales report:<br>
    <input type=button value=January onclick=SalesReport3(1)>
    <input type=button value=February onclick=SalesReport3(2)>
    <input type=button value=March onclick=SalesReport3(3)>
    <input type=button value=April onclick=SalesReport3(4)>
    <input type=button value=May onclick=SalesReport3(5)>
    <input type=button value=June onclick=SalesReport3(6)>
    <input type=button value=July onclick=SalesReport3(7)>
    <input type=button value=August onclick=SalesReport3(8)>
    <input type=button value=September onclick=SalesReport3(9)>
    <input type=button value=October onclick=SalesReport3(10)>
    <input type=button value=November onclick=SalesReport3(11)>
    <input type=button value=December onclick=SalesReport3(12)>
    </body>
    </html>