Trying to use Vbscript to find data within excel and select its value from a dropdownlist within an HTA

StackOverflow https://stackoverflow.com/questions/18873087

Question

I would first like to add that I am very new to vbscript so sorry if this is something simple that I am missing. Ive searched but having issues coming up with the correct code (although this site has helped me a tremendous amount).

Anyway, I have created an HTA that you can enter data into a form and have it populate an excel spreadsheet. If the serial number is already in the spreadsheet then it updates the column, otherwise it adds a new column. All this is working fine. I am trying to add a SUB so i can have a search button to populate the fields so you can see what data is already in there before updating it. I am able to get the text box's to populate but the dropdown lists fail to select the matching value. Here is the section of the code that populates the form. If i change the dropdowns to text input then it populates fine. I placed to arrows to the left of the two lines that are failing, I have tried many different things but they all seem to fail. Hopefully that all makes sense. Thanks in advance!

  Sub SearchINV()
    Dim FSO, oExcel, oData, FoundCell, FindTag, FilePath, oWorkSheet

    FindTag = document.all.serial.value
    FilePath = "C:\file.xlsx"


    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oExcel = CreateObject("Excel.Application")
    Set oData = oExcel.Workbooks.Open(FilePath)
    oData.Worksheets("sheet1").Select
    Set FoundCell = oData.Worksheets("sheet1").Range("A1:A20000").Find(FindTag)
    oExcel.DisplayAlerts = False

    Set FoundCell = oData.Worksheets("sheet1").Range("A1:A20000").Find(FindTag)
    If Not FoundCell Is Nothing Then
      Dim r, c
      r = FoundCell.Row
      c = FoundCell.Column  
      Set oWorkSheet = oData.Worksheets("sheet1")
      document.getElementById("serial").value = FoundCell.Value
      document.getElementById("combination").value = oWorksheet.Cells(r, c+1).Value
      document.getElementById("last").value = oWorksheet.Cells(r, c+2).Value
      document.getElementById("first").value = oWorksheet.Cells(r, c+3).Value
      document.getElementById("department").value = oWorksheet.Cells(r, c+4).Value
--->  document.getElementById("floor").value = oWorksheet.Cells(r, c+5).Value
--->  document.getElementById("building").value = oWorksheet.Cells(r, c+6).Value
    Else
      MsgBox (FindTag & " not found")
    End If

    Set File_Path = nothing
    Set FindTag = nothing
    Set FoundCell = nothing
    oData.Close
    oExcel.Quit
    Set oWorkSheet = Nothing
    Set oData = Nothing
    Set oExcel = Nothing
    Set FSO = Nothing 
 End Sub
Was it helpful?

Solution

Since the information you provided is rather incomplete I can give you only a generic answer on how to populate dropdown lists.

A dropdown list in HTML is a <select> element, and the items listed in the dropdown list are <option> elements. The HTML code looks somewhat like this:

<select id="fruit">
  <option value="green">Apple</option>
  <option value="yellow" selected>Banana</option>
  <option value="red">Strawberry</option>
</select>

The presence of an attribute selected indicates that an option is selected.

To populate a dropdown list you need to add <option> elements to the <select> element, e.g. like this:

'sample data
data = CreateObject("Scripting.Dictionary")
data.Add "green" , "Apple"
data.Add "yellow", "Banana"
data.Add "red"   , "Strawberry"

'get dropdown list element
Set list = document.getElementById("fruit")

'populate dropdown list with sample data
For Each fruit In data.Keys
  Set opt = document.createElement("OPTION")
  opt.Text  = data(fruit)
  opt.Value = fruit
  list.Add opt
Next

If that doesn't help you need to provide more information about your source data, the elements you want to populate, and how the result should look like.


Edit: If you want to select an option from an already populated dropdown list if it matches the value of a given cell from an Excel spreadsheet you'd do it like this:

'get dropdown list element
Set list = document.getElementById("fruit")

'select matching element
For Each opt In list.Options
  If opt.Text = oWorksheet.Cells(r, c+5).Value Then opt.Selected = True
Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top