2013-08-05 86 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」部分也出現了一個錯字。 –

相關問題