2010-03-03 52 views
2

我正在尝试编写vba代码来填写web表单并为我单击按钮。我正在浏览页面上的各种option标签以选择我想要的标签。当我达到它时,我想选择它,但我不确定语法。HTMLInputElement被点击,HTMLOptionElement获取

Dim htmlO As HTMLOptionElement 
For Each htmlO In Object.getElementByTagName("option") 
    If Trim(htmlO.value) = "INS" Then 
     htmlO.???? (click? select?) 
     Exit For 
    End If 
Next 

下面是来自网页的HTML:

<select gtbfieldid="40" id="menu" name="pv_choice"> 
<option selected="selected" value="ZZZ">Choose Menu Option 
</option><option value="BL_COMP">Blanket Companies 
</option><option value="CARR_SEARCH">Carrier Search 
</option><option value="PASSWORD">Change Password 
</option><option value="FED_REG">FMCSA Register 
</option><option value="FEEDBACK">Feedback 
</option><option value="HOME">Home Page 
</option><option value="INS">Insurance Filing 
</option><option value="OOS_LIST">Out Of Service Carriers 
</option></select> 

我要选择选项 “INS”。

回答

0

到底是什么工作了......

昏暗htmlO作为HTMLSelect元
集HTMLS = objdoc.getElementById(“菜单”)
htmlS.selectedIndex = 7

我不得不引用整个菜单并选择选择哪一个,不是选择单个选项。

1

我没有用VBA所以我提前道歉,但假设它使用了DOM比其他语言我熟悉的相同的结构,尝试:

htmlO.selected= "selected" 

这是我会怎样做到这一点在JavaScript :)

HTH

+0

谢谢你的回答...但它不起作用。当我做htmlO.check时,菜单选项没有被选中。 – dmr 2010-03-03 18:45:37

+0

对不起,我是一个doofus,我以为你正在使用复选框。尝试selected =“选中” 更新我的原始回复。 – Gazillion 2010-03-03 18:53:05

+0

我也尝试过htmlO.select也没有工作......谢谢:) – dmr 2010-03-03 18:56:09

2
Dim StrFindText as string 
Dim htmlSelect As HTMLSelectElement 
Dim htmlItemOption As IHTMLOptionElement 

Set htmlSelect = Object.getElementById("menu") 
StrFindText = "INS" 

For i = 0 To htmlSelect.options.length - 1 
    htmlItemOption = htmlSelect.options(i) 
    ' UCase(htmlItemOption.text) = UCase(StrFindText) ' if find text options 
    If UCase(htmlItemOption.value) = UCase(StrFindText) Then 
     htmlItemOption.selected = True 
     Exit For 
    End If 
Next i