我需要根据我正在编写宏的excel书中的值从webform下拉列表中选择一个值。到目前为止,我已经能够导航到该网站并使用以下vba代码单击所需的选项卡:
Sub FillInternetForm()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "website I'm navigating to"
ie.Visible = True
While ie.Busy
DoEvents 'wait until IE is done loading page.
Wend
Set AllHyperLinks = ie.Document.getElementsByTagName("A")
For Each Hyper_link In AllHyperLinks
If Hyper_link.innerText = "Reconciliations" Then
Hyper_link.Click
Exit For
End If
Next
接下来,我需要根据我正在尝试编写宏的工作簿中的预定义值(单元格引用)单击“Preparer”,“Approver”或“Reviewer”。下面是html编码,我认为我需要在宏中引用以执行所描述的操作:
<td class="DashControlTableCellRight"><select name="ctl00$MainContent$ucDashboardPreparer$ucDashboardSettings$ctl00$ddlRoles" class="ControlDropDown" id="ctl00_MainContent_ucDashboardPreparer_ucDashboardSettings_ctl00_ddlRoles" onchange="OnRoleChanged(this);">
<option selected="selected" value="Preparer">Preparer</option>
<option value="Reviewer">Reviewer</option>
<option value="Approver">Approver</option>
</select></td>
非常感谢任何帮助。
最佳,
捐赠
答案 0 :(得分:6)
我首先要指出的是,单独使用ie.busy
是危险的。当您自动化网页时,.busy
非常不可靠,我建议您在循环中也包含.readyState
属性。
请参阅此测试我使用
使用循环运行.readyState < 4
:注意前5行的
.Busy
是如何为真,然后第6行为假?这是您的代码认为网页已加载的地方。但是,.readyState
仍为1 (相当于READYSTATE_LOADING
)突然之间它再次变得忙碌,直到
.readystate = 4
(READYSTATE_COMPLETE
)。
我已将您的.busy
方法移动到一个单独的子方法中,因为这是在浏览网页时经常调用的方法。
Sub ieBusy(ie As Object)
Do While ie.busy Or ie.readystate < 4
DoEvents
Loop
End Sub
Sub FillInternetForm()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "website I'm navigating to"
ie.Visible = True
iebusy ie
Set AllHyperLinks = ie.document.getElementsByTagName("A")
For Each Hyper_link In AllHyperLinks
If Hyper_link.innerText = "Reconciliations" Then
Hyper_link.Click
Exit For
End If
Next
iebusy ie
Dim mySelection As String, mySelObj As Object
Set mySelObj = ie.document.getElementById("ctl00_MainContent_ucDashboardPreparer_ucDashboardSettings_ctl00_ddlRoles")
'set your selection here
mySelection = "Preparer" 'or Reviewer or Approver
Select Case mySelection
Case "Preparer", "Reviewer", "Approver"
Case Else
MsgBox "Invalid Selection!"
ie.Quit
Exit Sub
End Select
mySelObj.Value = mySelection
End Sub
mySelObj
是将设置您的选择的对象。