Search code examples
vbscriptdropdownbox

Read options from drop-down list into an array


I'm trying to write code (in Macro Express Pro) to read all of the options in a drop-down box and set them to an array in this format:

;option 1;option 2;option 3;option 4;option 5

So far I was able to make it display in a MsgBox (just as a test, I don't want the message box in production) ,but not append them to an array. Here is what I'm working with (from the site):

<select id="ctl00_cphContent_ddlWorkQueue" class="ddlbox">
    <option value="4449">option 1</option>
    <option value="4370">option 2</option>
    <option value="4371">option 3</option>
    <option value="4372">option 4</option>
    <option value="4373">option 5</option>
</select>

From the VBS external script:

set OptionChooser = MyIE.Document.GetElementbyid("ctl00_cphContent_ddlWorkQueue")

For Each objOption in OptionChooser.Options
    Msgbox objOption.InnerText
Next

Solution

  • Create a dynamic array and fill it with the options, then Join the array:

    ReDim arr(OptionChooser.Options.Length - 1)
    For i = 0 To OptionChooser.Options.Length - 1
      arr(i) = OptionChooser.Options(i).Text
    Next
    str = Join(arr, ";")
    
    MsgBox str