Gra*_*ter 2 html excel vba excel-vba dropdown
我需要根据我正在编写宏的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
Run Code Online (Sandbox Code Playgroud)
接下来,我需要根据我正在尝试编写宏的工作簿中的预定义值(单元格引用)单击"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>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
最好,
格兰特
我首先要指出的ie.busy是,单独使用是危险的..busy当您自动化网页时非常不可靠,我建议您.readyState在循环中也包含该属性.
看到这个测试我使用循环运行
.readyState < 4:请注意
.Busy前5行的情况如何,然后在第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
Run Code Online (Sandbox Code Playgroud)
这mySelObj是将设置您的选择的对象.