我的VBA在调试模式下工作,但在运行模式下不工作

时间:2018-01-17 17:53:07

标签: vba

我在VBA中编写代码以打开Internet Explorer网站,在表单中填入一个数字,然后点击" pesquisar'''按钮。在那之后,我需要点击" partes"按钮。 我的代码在调试模式下完美运行,但是当我运行它时,它不起作用。 请帮帮我!

这是我的代码:

numero = Application.InputBox("Por Favor entre com o numero precatorio", "", "")


Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "https://processual.trf1.jus.br/consultaProcessual/numeroProcesso.php?secao=TRF1&enviar=ok"

Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop

ie.Document.all("proc").innerText = numero
ie.Document.getElementById("enviar").Click

Set objElementCol = ie.Document.getElementsByTagName("a")

For Each objElement In objElementCol
    DoEvents
    If objElement.href = "https://processual.trf1.jus.br/consultaProcessual/processo.php?proc=1219012520174019198&secao=TRF1&pg=1&enviar=Pesquisar#aba-partes" Then
     DoEvents
        objElement.Click
        Exit For
    End If
    DoEvents
Next objElement

1 个答案:

答案 0 :(得分:0)

在导航网页时,我创建了这个例程,你只需要等待网页完成加载。

Sub ieBusy(ieObj As InternetExplorer)
    With ieObj
        Do While .Busy Or .readyState < READYSTATE_COMPLETE
            DoEvents
        Loop
    End With
End Sub

因此,在您的代码中,您可以使用ieBusy ie

Option Explicit

Sub ieBusy(ieObj As InternetExplorer)
    With ieObj
        Do While .Busy Or .readyState < READYSTATE_COMPLETE
            DoEvents
        Loop
    End With
End Sub


Sub test()

    ' Make sure you Declare your variables, such as <numero>
    ' You other code here

    numero = Application.InputBox("Por Favor entre com o numero precatorio", "", "")

    Dim ie As New InternetExplorer
    ie.Visible = True
    ie.navigate "https://processual.trf1.jus.br/consultaProcessual/numeroProcesso.php?secao=TRF1&enviar=ok"

    ieBusy ie

    ie.document.all("proc").innerText = numero
    ie.document.getElementById("enviar").Click

    ' It will become busy again after your click, so make another call
    ieBusy ie

    Set objElementCol = ie.document.getElementsByTagName("a")

    For Each objElement In objElementCol
        DoEvents
        If objElement.href = "https://processual.trf1.jus.br/consultaProcessual/processo.php?proc=1219012520174019198&secao=TRF1&pg=1&enviar=Pesquisar#aba-partes" Then
         DoEvents
            objElement.Click
            Exit For
        End If
        DoEvents
    Next objElement

End Sub
  

注意:我在代码顶部添加了Option Explicit。您应该养成始终的习惯,将其添加到每个模块的顶部。这将要求您声明所有变量。