【问题标题】:VBA code to select a value from webform dropdown based on value in excel workbookVBA代码根据excel工作簿中的值从webform下拉列表中选择一个值
【发布时间】:2018-07-07 10:25:19
【问题描述】:

我需要根据我正在编写宏的 excel 书中的值从网络表单下拉列表中选择一个值。到目前为止,我已经能够导航到该网站并使用以下 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

接下来,我需要根据我尝试编写宏的工作簿中的预定义值(单元格引用)单击“准备者”、“批准者”或“审阅者”。下面是我认为我需要在宏中引用以执行所描述的操作的 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>

任何帮助将不胜感激。

最好的,

授予

【问题讨论】:

    标签: html vba excel dropdown


    【解决方案1】:

    我首先要指出的是,单独使用ie.busy 是很危险的。 .busy 在您自动化网页时非常不可靠,我建议您在循环中也包含 .readyState 属性。

    查看我使用.readyState &lt; 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
    

    mySelObj 是设置您选择的对象。

    【讨论】:

    • K.Davis - 非常感谢您在回答我的问题时提供的详细信息。当我调试时,我在以下代码行中收到以下错误
    • 错误:远程服务器机器不存在或无效
    • 代码行:设置 mySelObj = ie.Document.getElementsByName("ctl00$MainContent$ucDashboardPreparer$ucDashboardSettings$ctl00$ddlRoles")(0)
    • @GrantBangerter 您能否重新检查您提供的 HTML 中的该名称是否已更改?如果是这样,我们将需要考虑不同的路线。此外,通常该错误意味着您关闭了 ie - 您可能需要设置 ie.visible = False 以免意​​外关闭它。
    • @K.Davis - 您的最新更新解决了我的问题。非常感谢您的帮助和及时回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多