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

前端 未结 1 680
南笙
南笙 2020-12-20 05:46

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

相关标签:
1条回答
  • 2020-12-20 06:10

    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:

    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.

    0 讨论(0)
提交回复
热议问题