Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

html - VBA code to select a value from webform dropdown based on value in excel workbook

I'm needing to select a value from a webform dropdown based on the value in the excel book where I'm writing the macro. So far I've been able to navigate to the website and click the desired tab with the following vba code:

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

Next, I'm needing to click either "Preparer", "Approver", or "Reviewer" based on a predefined value (cell reference) within the workbook where I'm attempting to write the macro. Below is the html coding that I believe I need to reference within my macro to perform the action described:

<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>

Any help would be greatly appreciated.

Best,

Grant

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I first want to point out that using ie.busy by itself is dangerous. .busy is very unreliable when you are automating web pages, and I would recommend that you also include the .readyState property in your loop.

See this test I ran using a loop using .readyState < 4:

enter image description here

enter image description here

Notice how .Busy was true for the first 5 lines, then became false on line 6? This is where your code would have thought the webpage was loaded. However, .readyState was still 1 (which is the equivalent to READYSTATE_LOADING)

All of a sudden it became busy again until .readystate = 4 (READYSTATE_COMPLETE).

I have moved your .busy method into a separate sub because this is something that is called quite often when navigating web pages.

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

The mySelObj is the object that will set your selection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...