Excel VBA无法加载Internet Explorer

我有一个本地网页列表(超过9000),我想用Excel VBAparsing。 我在IE 11上使用Office 2013:

  • 一个Windows 7企业Pro x64,16 GB RAM,i7 – 处理器,但也
  • Windows 8.1企业版x64,12 GB RAM,i7处理器

这两个machiens的问题是,成功parsing约70-80页后,程序突然无法加载到IE的下一个网页。 它被“卡住”这样说(见下面的代码中的评论)。 如果我重置程序,那么它可以在“失败”之后再parsing70-80个configuration文件。

[编辑:对不起,我错误地张贴了错误的代码。 这是正确的版本]

这是代码的一部分:

<!-- language: lang-HTML --> Sub ImportFromWebpage() 'GLOBAL VARIABLES Dim html As HTMLDocument Dim CurrentRowPosition, ProfileNumber, TotalProfiles As Integer Dim TempProfileID As String Dim profileRange, posCounter, currentProfile As Range Set profileRange = Worksheets("List_of_Files").Range("B2:B20000") ProfileNumber = 519 CurrentRowPosition = 520 TotalProfiles = Application.WorksheetFunction.CountA(profileRange) 'MsgBox "TotalProfiles = " & TotalProfiles 'VARIABLES NEEDED FOR PARSING HERE 'ELEMENTS Dim firstIHTMLElmt, secondIHTMLElmt, thirdIHTMLElmt As IHTMLElement Dim firstTempIHTMLElmt, secondTempIHTMLElmt, thirdTempIHTMLElmt, fourthTempIHTMLElmt, fiftTempIHTMLElmt As IHTMLElement 'COLLECTIONS Dim firstIHTMLEColl, secondIHTMLEColl, thirdIHTMLEColl As IHTMLElementCollection Dim firstTempIHTMLEColl, secondTempIHTMLEColl, thirdTempIHTMLEColl, fourthTempIHTMLEColl, fifthTempIHTMLEColl As IHTMLElementCollection Dim ie As InternetExplorerMedium Set ie = New InternetExplorerMedium ie.Visible = False 'FROM HERE LOOPING For startNumber = 1 To TotalProfiles Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles" 'Set currentProfile = Worksheets("List_of_Files").Range("J" & CurrentRowPosition) // OLD Set currentProfile = Worksheets("List_of_Files").Range("B" & CurrentRowPosition) ie.navigate currentProfile Application.StatusBar = "Loading profile: " & ProfileNumber & "; file location: " & currentProfile Do While ie.READYSTATE <> READYSTATE_COMPLETE DoEvents Loop Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement" Set html = ie.document Set ie = Nothing [code, code, code, code ...] Application.Wait (Now + TimeValue("0:00:02")) Next startNumber Set html = Nothing ie.Quit Set ie = Nothing MsgBox "Done parsing all profiles!" End Sub 

下面是Windows 8.1任务pipe理器在加载失败后的屏幕截图: 在这里输入图像说明

在这里输入图像说明

剂量有什么线索,为什么发生这种情况? 不仅在一个machiene,但在两个。

我不是一个编程经验,甚至更less的VBA,所以任何帮助将不胜感激。

这个解决scheme对我来说是个不错的解决scheme。 不知道这是否是最好的解决scheme,但它对我来说非常有用。

  • 在循环之前放置IE declaration来启动Internet Explorer的一个实例; 这是将被使用的唯一实例(链接将在此实例中刷新)
  • 在循环中set html = Nothing
  • 在循环之外set ie = Nothing ,这样只有链接可以刷新而不用重新启动IE
  • ie.Quit .parsing所有> 9000网页后才ie.Quit (所以在循环之外)

希望它能帮助其他人解决同样的问题。

 Sub ImportFromWebpage() 'GLOBAL VARIABLES Dim html As HTMLDocument Dim CurrentRowPosition, ProfileNumber, TotalProfiles As Integer Dim TempProfileID As String Dim profileRange, posCounter, currentProfile As Range Set profileRange = Worksheets("List_of_Files").Range("B2:B20000") ProfileNumber = 1 CurrentRowPosition = 2 TotalProfiles = Application.WorksheetFunction.CountA(profileRange) 'MsgBox "TotalProfiles = " & TotalProfiles Dim ie As InternetExplorerMedium Set ie = New InternetExplorerMedium ie.Visible = False 'FROM HERE LOOPING For startNumber = 1 To TotalProfiles Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles" 'Set currentProfile = Worksheets("List_of_Files").Range("J" & CurrentRowPosition) // OLD Set currentProfile = Worksheets("List_of_Files").Range("B" & CurrentRowPosition) ie.navigate currentProfile Application.StatusBar = "Loading profile: " & ProfileNumber & "; file location: " & currentProfile Do While ie.READYSTATE <> READYSTATE_COMPLETE DoEvents Loop Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement" Set html = ie.document [code, code, code, code ...] Set html = Nothing Application.Wait (Now + TimeValue("0:00:02")) Next startNumber Set ie = Nothing ie.Quit MsgBox "Done parsing all profiles!" End Sub