Reputation: 381
In the following
<select id="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
How can I get the text of the selected option (i.e. "Test One" or "Test Two") using VBA?
The exact same question was asked for JavaScript here: Retrieving the text of the selected <option> in <select> element
and the solution given was
function getSelectedText(elementId) {
var elt = document.getElementById(elementId);
if (elt.selectedIndex == -1)
return null;
return elt.options[elt.selectedIndex].text;
}
var text = getSelectedText('test');
I've created the following version in VBA, but I get an object doesn't support this property or method error at the if statement.
Function getSelectionText(SelectionObject)
If SelectionObject.selectedIndex = -1 Then getSelectionText = ""
getSelectionText = SelectionObject.Options(selection.selectedIndex).Text
End Function
There's to much code to post the call, but the function is definitely being passed a selection object.
Thanks for your help!
Upvotes: 1
Views: 4138
Reputation: 166351
Your function doesn't exit if selectedIndex is -1...
Function getSelectionText(SelectionObject)
If SelectionObject.selectedIndex = -1 Then
getSelectionText = ""
Else
getSelectionText = SelectionObject.Options(SelectionObject.selectedIndex).Text
End If
End Function
Upvotes: 1