How Can I get the Actual values of a List Box or Dropdown List Option from this HTA to a txt file

1 week ago 17
ARTICLE AD BOX

You need to assign an id to each element so that you can get its value into a variable. For example, if your "Country" element has id=Country, then you can use this VBScript code:

sCountry = Country.options[Country.selectedIndex].value

or more simply:

sCountry = Country.value

Then you can use the Scripting.FileSystemObject to write the variables out to a text file.

Since you're just getting and saving data, an intermediate variable can be skipped. That is, you can write Country.value directly to the file.

Below are examples using VBScript and JScript. Since VBScript will no longer be installed by default starting sometime in 2027, it is better to use JScript.

Note that the original HTA in the question only has an <html> header which will cause the HTA to run in IE 5 mode. The header in the examples below will make the HTA run in IE 9 mode. That's the highest mode that allows use of the hta:application section. However, the only non-default setting in the original HTA is showintaskbar=no. Generally you should allow the program to appear in the taskbar, so that could be removed. In which case, it's okay to change IE=9 to IE=11 in the header to be able to use the latest formatting supported by MSHTML.

If you use AI to help you enhance this code, be sure to remind the AI about the IE level you're using so that it generates compatible code.

Example1.hta (VBScript)

<!DOCTYPE html> <html> <head> <title>Enter and save data</title> <meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9"> <hta:application showintaskbar=no > <script language=VBScript> w = 500 h = 450 x = (screen.availWidth - w) / 2 y = (screen.availHeight - h) / 2 window.resizeTo w, h window.moveTo x, y Sub SaveData() Set oFSO = CreateObject("Scripting.FileSystemObject") folder = "C:\Temp" filePath = folder & "\Test.txt" If Not oFSO.FolderExists(folder) Then oFSO.CreateFolder folder Set oFile = oFSO.CreateTextFile(filePath, True) output = "Username = " & UserName.value & vbCrLf & _ "Email = " & Email.value & vbCrLf & _ "Title = " & Title.value & vbCrLf & _ "Company = " & Company.value & vbCrLf & _ "Product = " & Product.value & vbCrLf & _ "Country = " & Country.value & vbCrLf & _ "region = " & Region.value & vbCrLf oFile.Write output oFile.Close MsgBox "Saved to " & filePath End Sub </script> <style> body {font-family:"Comic Sans MS"; font-size:10pt} label {display:inline-block; width:6em} input, select {width:20em; margin-bottom:1em} button {display:block; width:8em; height:2em; margin:auto} </style> </head> <body> <label for="UserName">User name:</label> <input type="text" id="UserName"><br> <label for="Email">Email:</label> <input type="text" id="Email"><br> <label for="Title">Title:</label> <input type="text" id="Title"><br> <label for="Company">Company:</label> <input type="text" id="Company"><br> <label for="Product">Product:</label> <input type="text" id="Product"><br> <label for="Country">Country:</label> <select id="Country"> <option>UK</option> <option>USA</option> <option>Ireland</option> <option>Jersey</option> </select><br> <label for="Region">Region:</label> <select id="Region"> <option>West Midlands</option> <option>South West</option> <option>South East</option> <option>East Midlands</option> </select><br><br> <button onclick="SaveData()">Save</button> </body> </html>

Example2.hta (JScript)

<!DOCTYPE html> <html> <head> <title>Enter and save data</title> <meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9"> <hta:application showintaskbar=no > <script> var w = 500; var h = 450; var x = (screen.availWidth - w) / 2; var y = (screen.availHeight - h) / 2; window.resizeTo(w, h); window.moveTo(x, y); function SaveData() { var oFSO = new ActiveXObject("Scripting.FileSystemObject"); var folder = "C:\\Temp"; var filePath = folder + "\\Test.txt"; if (!oFSO.FolderExists(folder)) { oFSO.CreateFolder(folder); } var oFile = oFSO.CreateTextFile(filePath, true); var output = "Username = " + UserName.value + "\r\n" + "Email = " + Email.value + "\r\n" + "Title = " + Title.value + "\r\n" + "Company = " + Company.value + "\r\n" + "Product = " + Product.value + "\r\n" + "Country = " + Country.value + "\r\n" + "region = " + Region.value + "\r\n"; oFile.Write(output); oFile.Close(); alert("Saved to " + filePath); } </script> <style> body {font-family:"Comic Sans MS"; font-size:10pt} label {display:inline-block; width:6em} input, select {width:20em; margin-bottom:1em} button {display:block; width:8em; height:2em; margin:auto} </style> </head> <body> <label for="UserName">User name:</label> <input type="text" id="UserName"><br> <label for="Email">Email:</label> <input type="text" id="Email"><br> <label for="Title">Title:</label> <input type="text" id="Title"><br> <label for="Company">Company:</label> <input type="text" id="Company"><br> <label for="Product">Product:</label> <input type="text" id="Product"><br> <label for="Country">Country:</label> <select id="Country"> <option>UK</option> <option>USA</option> <option>Ireland</option> <option>Jersey</option> </select><br> <label for="Region">Region:</label> <select id="Region"> <option>West Midlands</option> <option>South West</option> <option>South East</option> <option>East Midlands</option> </select><br><br> <button onclick="SaveData()">Save</button> </body> </html>
Read Entire Article