有所需数据时,阻止IE进一步加载

时间:2018-07-02 08:54:47

标签: html excel-vba internet-explorer vba excel

当所需数据已经显示时,我正试图停止IE,因为否则即使我能看到.Visible=True上的所有内容,它也会继续加载。

我想将某种变体(SOMETHING)传递给加载sub的网页,它将相应地测试IHTMLElement

我意识到我无法在加载页面之前预先定义IHTMLElement并传递它。正在四处搜寻,但找不到任何解决方案。目前,这是我所不知道的,我想弄清楚该怎么做。

Private ie_browser As InternetExplorer

Private Sub Download_Test()

   Dim str_url As String
   str_url = "..."
   Set ie_browser = New InternetExplorer
   ie_browser.Visible = True

   'here I want pass that spec variant to the Sub
   Call Load_WebPage(str_url, SOMETHING)

End Sub

Private Sub Load_WebPage( _
   ByVal str_url As String, _
   ByVal SOMETHING _
   )

   Dim html_data_element As IHTMLElement
   Dim int_counter_sleep As Integer

   ie_browser.Navigate str_url
   For int_counter_sleep = 1 To 60     '60 x 500 = 30000 ms = 30 sec
      If ie_browser.Busy Or ie_browser.READYSTATE <> 4 Then

         'SOMETHING -> html_data_element
         'here I want to use that spec variant to define said IHTMLElement

         If Not html_data_element Is Nothing Then
            ie_browser.Stop
            Exit For
         End If

         Sleep 500
      Else
         Exit For
      End If
   Next int_counter_sleep

End Sub

1 个答案:

答案 0 :(得分:0)

学习了几个小时后,我设法通过一个附加功能解决了该问题,该功能按名称引用并使用Application.Run来执行。

Private ie_browser As InternetExplorer

Private Sub Download_Test()
    Dim str_url As String
    str_url = "..."
    Set ie_browser = New InternetExplorer
    ie_browser.Visible = True
    Load_WebPage str_url, "Get_webpage_readiness"   'passing only the name of the function
End Sub

Private Sub Load_WebPage( _
    ByVal str_url As String, _
    Optional ByVal str_get_readiness_function_name As String _
    )
    Dim int_counter_sleep As Integer
    Dim bool_readiness As Boolean
    ie_browser.Navigate str_url
    For int_counter_sleep = 1 To 60
        If ie_browser.Busy Or ie_browser.READYSTATE <> 4 Then

            'Referencing the function by name
            If str_get_readiness_function_name <> vbNullString Then
                bool_readiness = Application.Run(str_get_readiness_function_name)
                If bool_readiness Then
                    ie_browser.Stop
                    Exit For
                End If
            End If

            Sleep 500
        Else
            Exit For
        End If
    Next int_counter_sleep
End Sub

Private Function Get_webpage_readiness() As Boolean
    Dim html_data_element As IHTMLElement
    On Error Resume Next    'skipping the error while the webpage hasn't loaded yet
    Get_webpage_readiness = False
    Set html_data_element = ie_browser.Document. ...
    If Not html_data_element Is Nothing Then
        Get_webpage_readiness = True
    End If
End Function