0

我正在编写一个 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

是什么赋予了?为什么会冻成这样?它与这个问题有什么关系吗?(请尽量不要嘲笑我的无知!)非常感谢任何帮助。

编辑:见上面的斜体文本。我没有完全准确地描述问题!

4

2 回答 2

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

或者只是在浏览器之间等待。导航调用一段时间,让浏览器有足够的时间加载一个文档,然后再开始加载下一个文档。尝试不同的时间段并观察是否可以通过这种方式避免冻结问题。

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
于 2012-11-07T18:29:02.320 回答
0

好吧,我也是新人,但据我所知,我会在子末尾设置 ie = Nothing 以放松 VBA 和 InternetExplorer 应用程序之间的任何关系

于 2013-12-03T18:56:16.873 回答