1

I've looked through all other questions, plus many searches over the internet, and cannot find anything that works with my situation.

I have a macro that was developed in VBScript for IBM Personal Communications (PCOMM). My bosses want me to convert it so it can be used in Macro Express Pro (ME). ME is a WYSIWYG (what you see is what you get) editor that has preset functions and allows for external scripting of JScript, VBScript, and HTA.

The PCOMM macro is about 1000 lines of pure VBScript, so it only makes sense to keep it that way, however ME doesn't allow some things that PCOMM does, so some modifications need to be made.

One modification is there already is one global instance of Internet Explorer (IE), so the macro has been designed to use that instance. I'm trying to get the selected option of a drop down box, but everything I've tried has been unsuccessful.

Here's the HTML code:

<select name="ctl00$cphContent$ddlWorkQueue" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphContent$ddlWorkQueue\',\'\')', 0)" id="ctl00_cphContent_ddlWorkQueue" class="ddlbox">
    <option selected="selected" value="5422">605</option>
    <option value="5419">ACCUM</option>
    <option value="5418">CORRESPONDENCE</option>
    <option value="5415">FEKEY</option>
    <option value="5416">NKPORTAL</option>
    <option value="5420">PROVIDER</option>
    <option value="5423">EDITS</option>
    <option value="5421">TRACR</option>
    <option value="5417">WAND</option> 
</select>

As you can see, "605" is selected. I want to be able to display "605" in a message box. Here's the VBScript for this:

'This subroutine checks to see if the selected queue matches the ini value if it does not it updates the ini value
Sub SetQueue
    Dim Selection

    'Commented out by previous developer.  We do not want to create a new instance of Internet Explorer.  We want to use the existing instance...
    'Set objIE = CreateObject("InternetExplorer.Application")

    Selection = Document.getElementById("ctl00_cphContent_ddlWorkQueue").selectedIndex
    MsgBox "Selection: " & Selection
End Sub

Hopefully someone can offer me some assistance and point me in the right direction. All I get when I run the macro is "Selection: ".

Lou
  • 389
  • 3
  • 20
  • 38

1 Answers1

3

Any of these methods should work:

' Method 1 - Get the current HTML displayed within the <select> element:
MsgBox Document.getElementById("ctl00_cphContent_ddlWorkQueue").innerHTML

' Method 2 - Get the current text displayed within the <select> element:
MsgBox Document.getElementById("ctl00_cphContent_ddlWorkQueue").innerText

' Method 3 - Use the selected index to look up the selected option:
Set e = Document.getElementById("ctl00_cphContent_ddlWorkQueue")
MsgBox e.Options(e.selectedIndex).Text

' Method 4 - Loop through all options looking for the selected one:
For Each o In Document.getElementById("ctl00_cphContent_ddlWorkQueue").Options
    If o.Selected Then MsgBox o.Text
Next
Bond
  • 16,071
  • 6
  • 30
  • 53
  • Tried all of them...still no luck. It's like the code is unable to read the drop down box, but I copied and pasted it from the source. – Lou Jun 19 '14 at 15:23
  • Are you using `On Error Resume Next` anywhere in your code? – Bond Jun 19 '14 at 15:26
  • Yes, it's directly above it with the comment `'If the user is not on the appropriate screen the macro will throw an error`. I commented that out and now I don't get the message box at all. I need the message box so I can see if I'm getting the text from the option. – Lou Jun 19 '14 at 15:28
  • Since I only have a small view of your code, it's tough for me to guess at what might be happening. `On Error Resume Next` may be causing your script to fly through any errors and call your `SetQueue` function regardless. With errors on, your code may be taking a different path. The four methods above should work, though. If they don't then I don't think your problem has to do with – Bond Jun 19 '14 at 16:17
  • I am able to get the selectedIndex with this line of code: `objIE.Document.Forms(0).Elements.All("ctl00_cphContent_ddlWorkQueue").selectedIndex`. Per my HTML code, if I use the above line of code to get the selectedIndex, I get "0", which is correct...I just can't get the text for that index. If I change ".selectedIndex" to ".innerText", I get all the option texts within the whole ` – Lou Jun 19 '14 at 17:38
  • 1
    I must have had something not spelled correctly. This option worked: `For Each objOption In objIE.Document.Forms(0).Elements.All("ctl00_cphContent_ddlWorkQueue").options If objOption.Selected Then MsgBox objOption.Text Next`. Thank you VERY much!! :D – Lou Jun 19 '14 at 18:02