2013-08-05 88 views
1

在下面获取选择选项的文本中选择元素使用VBA

<select id="test"> 
<option value="1">Test One</option> 
<option value="2">Test Two</option> 
</select> 

我怎样才能获得所选择的选项(即“测试一”或“测试二”)使用VBA的文字?

确切同样的问题被问对JavaScript这里:Retrieving the text of the selected <option> in <select> element

和解决方案给予了

function getSelectedText(elementId) { 
    var elt = document.getElementById(elementId); 

    if (elt.selectedIndex == -1) 
     return null; 

    return elt.options[elt.selectedIndex].text; 
} 

var text = getSelectedText('test'); 

我创建了一个VBA以下的版本,但我得到的对象不支持if语句中的此属性或方法错误。

Function getSelectionText(SelectionObject) 

If SelectionObject.selectedIndex = -1 Then getSelectionText = "" 

getSelectionText = SelectionObject.Options(selection.selectedIndex).Text 

End Function 

有得多的代码后调用,但功能绝对是传递一个选择对象。

感谢您的帮助!

回答

1

你的功能不会退出,如果是的selectedIndex -1 ...

Function getSelectionText(SelectionObject) 

If SelectionObject.selectedIndex = -1 Then 
    getSelectionText = "" 
Else 
    getSelectionText = SelectionObject.Options(SelectionObject.selectedIndex).Text 
End If 

End Function 
+0

感谢,将有绝对进一步细分功能上下行的反应,但我仍然得到对象没有按” t在if语句上支持此属性或方法错误。这就像selectedIndex在选择对象应该是时不是选择对象的属性。 – ebrts

+0

'selectedIndex'肯定是一个有效的属性,所以看起来可能被传入的对象不是你认为的那样。如果你把手表放在那个变量上,你会看到什么?在您从HTML获取对“SelectionObject”的引用时,可能会包含VBA。注意:在我的帖子中,我在功能的“Else”部分也出现了一个错字。 –

相关问题