userform中的Web浏览器控件:如何等待页面初始化

时间:2014-12-26 23:45:05

标签: vba excel-vba webbrowser-control excel

任务:使用Excel VBA导航到网站,登录并转到输入页面。 在该页面上,按顺序输入存储在Sheet1中的列中的一系列值。

到目前为止我做了什么: 我创建一个webbrowser控件并导航到该网站并停止。 然后单击Sheet1上的一个按钮,其中包含将进行输入的宏,存储在模块中。

发生了什么: 控件很好地导航并导航到预期的站点。 (这是用户格式代码) 单击按钮,它将从电子表格中获取用户ID和密码,输入它们,单击登录按钮,一切都很顺利。 但是,下一个陈述是:

Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_ddlBus")

并且inputfield显示为空。

如果我停止执行并逐步执行它,它就会起作用。 我试过Application.Wait;对于x = 1到5000000; On Error Goto / Resume并继续尝试,但nothings有效。

我也试过.NavigateComplete,.DocumentCompleted以及其他人,但我收到错误,说成员不受支持。

我在我的智慧结束 - 我真是太近了!!到目前为止,我花了更多的时间来保存它,但现在它是个人的!谢谢你的帮助。

这是来自另一个初始化控件的网站的借用代码。

Private Sub UserForm_Initialize()

    Dim a, c As Integer

    With Me
        .StartUpPosition = 0
        .Top = 150
        .Left = -700
    End With

    With Me.objWebBrowser
        .Navigate2 "http://www.schoolbuscity.com/Mapnetweb_47/login.aspx"
        .Visible = True
    End With
End Sub


Private Sub GetSheets()

'this is my code

    Dim inputfield As Object

    Dim SendText As String

    Dim NumberOfRoutes, r, errCount As Integer

    errCount = 0

    NumberOfRoutes = Range("NumberOfRoutes")
    ReDim RouteNumbers(NumberOfRoutes) As String

    For r = 1 To NumberOfRoutes
        RouteNumbers(r) = Cells(r, 1).Value
    Next r

'    Sheets("Sheet1").Select

    Range(Cells(5, 2), Cells(6, 2)).ClearContents ' this indicates success for the chosen cells

    SendText = Range("userid").Value
    Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("Login1_UserName")
    inputfield.Value = SendText

    SendText = Range("password").Value
    Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("Login1_Password")
    inputfield.Value = SendText

    Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("Login1_Login")
    Application.Wait (Now + TimeValue("0:00:01"))
    inputfield.Click

    Application.Wait (Now + TimeValue("0:00:01")) ' I've tried waiting for up to 10 seconds

    Set inputfield = Nothing

On Error GoTo TryAgain

    For r = 5 To 6 'NumberOfRoutes ' just want to use 2 loops for testing

' this is where is fails, I believe, because the page is not initialized

' but if waiting is not the answer, then what is?

        Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_ddlBus")
        inputfield.Value = RouteNumbers(r)

        Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_btnGo")
        inputfield.Click

        Application.Wait (Now + TimeValue("0:00:01"))
        Cells(r, 2).Value = "Sent"

'        WebBrowser.objWebBrowser.Document.Print

        WebBrowser.objWebBrowser.GoBack
    Next r

GoTo EndIt

TryAgain:

    Set inputfield = Nothing

    Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_ddlBus")

    errCount = errCount + 1

    If errCount > 5 Then GoTo EndIt

    Resume

EndIt:

If errCount > 0 Then

MsgBox "errCount= " + CStr(errCount)

Else

MsgBox "Did it"

End If

End Sub

2 个答案:

答案 0 :(得分:3)

这是waitbin vbscript的方法。

     Set ie = CreateObject("InternetExplorer.Application") 
     ie.Visible = 0
     'must navigate to a local file to avoid security prompts
     ie.Navigate2 "C:\Users\User\Desktop\Filter.html"
     Do 
         wscript.sleep 100
     Loop until ie.document.readystate = "complete" 

答案 1 :(得分:0)

VBA中的示例:

{{1}}