打开几个新选项卡时,IWebBrowser2 :: Navigate2偶尔会冻结

时间:2012-11-07 17:36:03

标签: vba excel-vba internet-explorer-8 excel-2007 excel

我正在编写Excel脚本以在Internet Explorer选项卡中打开PDF列表。它在大多数情况下工作正常,但偶尔当我尝试关闭浏览器窗口时,一些选项卡将关闭,然后停止并且所有IE实例将冻结,所以我必须杀死它们全部在任务管理器中。 请注意,我可以通过单独关闭每个标签来避免此问题

我正在运行IE8和Excel 2007,以备记录。这是我的代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    ShowBrowserWarning
    Dim TheHTML As String, PDFs, PDF, First, SerialValue, Test, k
    If Target.Column = 1 And Target.Count = 1 Then
        ' Get the serial number from the adjacent column
        SerialValue = Cells(Target.Row, Target.Column + 1)
        TheHTML = ShowHTML("http://ucmwww.dnr.state.la.us/ucmsearch/findAllDocuments.aspx?brief=False&query=xwellserialnumber+LIKE+'" & SerialValue & "'+AND+xdocumenttype+LIKE+'WELL ENGINEERING/MECHANICAL'&format=HTML&sortfield=xdate")
        Set PDFs = ExtractPDFs(TheHTML)
        If PDFs Is Nothing Then
            MsgBox "No associated well engineering/mechanical PDFs."
        Else
            First = True
            Dim ie As Object
            Set ie = CreateObject("InternetExplorer.Application")
            ie.Visible = True
            For Each PDF In PDFs
                'While ie.Busy
                '    Dim testvar
                '    testvar = 1 + 1
                'Wend
                If First Then
                    ' Open new IE window
                    ie.Navigate2 PDF.Value
                    First = False
                Else
                    ' Open tab in existing IE window
                    ie.Navigate2 PDF.Value, 2048
                End If
            Next
        End If
    End If
End Sub

是什么给出的?它为什么会这样冻结?它有什么事要做this issue吗? (请尽量不要嘲笑我的无知!)非常感谢任何帮助。

编辑:请参阅上面的斜体文字。我没有准确地描述这个问题!

2 个答案:

答案 0 :(得分:1)

那么浏览器忙碌的检查呢?它有助于避免这个问题吗?

    For Each PDF In PDFs

        While ie.Busy
            DoEvents
        Wend

        If First Then
            ' Open new IE window
            ie.Navigate2 PDF.Value
            First = False
        Else
            ' Open tab in existing IE window
            ie.Navigate2 PDF.Value, 2048
        End If
    Next

或者只是在浏览器之间等待。在开始加载下一个之前,给浏览器提供足够的时间来加载一个dokument。尝试不同的时间段,观察是否可以通过这种方式避免冻结问题。

For Each PDF In PDFs

  DoEventsForTimePeriod timePeriodInSeconds:=15 ' try different time periods here

  If First Then
  ' Open new IE window
  ie.Navigate2 PDF.Value
  First = False
  Else
  ' Open tab in existing IE window
  ie.Navigate2 PDF.Value, 2048
  End If
Next

Private Sub DoEventsForTimePeriod(ByVal timePeriodInSeconds As Single)
    ' VBA.Timer: Returns a Single representing the number of seconds elapsed since midnight.
    Dim pause As Single: pause = VBA.Timer + timePeriodInSeconds
    Do While VBA.Timer < pause
        DoEvents ' Yield to other processes.
    Loop
End Sub

答案 1 :(得分:0)

嗯,我也是新手,但是,据我所知,我会在sub的末尾设置ie = Nothing以松开VBA和InternetExplorer应用程序之间的任何关系

相关问题