[英]Excel VBA fails to load Internet Explorer
I have a list of local webpages (over 9000) which I want to parse with Excel VBA. 我有一个本地网页列表(超过9000),我想用Excel VBA解析。 I use Office 2013 with IE 11 on:
我使用Office 2013与IE 11:
The problem on both machiens is that after successfuly parsing about 70-80 pages, the programm suddenly fails to load the next webpage into IE. 两个机器人的问题是,在成功解析大约70-80页之后,程序突然无法将下一个网页加载到IE中。 It gets "stuck" so to say (see comment in the code below).
它被“卡住”所以说(见下面代码中的评论)。 If I reset the programm, then it can parse without problen again about 70-80 profiles after "failing" again.
如果我重置程序,那么它可以在再次“失败”之后再次解析70-80个配置文件。
[EDIT: I'm sorry, I posted by mistake the wrong code. [编辑:对不起,我错误地发错了代码。 Here is the corrected version]
这是更正后的版本]
Here is a part of the code: 这是代码的一部分:
<!-- 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
Here is a screenshot from the Windows 8.1 task manager AFTER failing to load : 以下是无法加载后 Windows 8.1任务管理器的屏幕截图:
Dose someone have any clue about why this is happening? 有人知道为什么会发生这种情况? Not only on one machiene, but on both.
不仅在一台机器上,而且在两者上。
I an not very experience with programming and even less with VBA so any help would be much appreciated. 我对编程的经验不是很好,甚至VBA也没什么经验,所以任何帮助都会非常感激。
This solution proved to be a good one in my case. 在我的案例中,这个解决方案被证明是一个很好的解 Don't know whether this is the best solution but it work very good for me.
不知道这是否是最好的解决方案,但它对我来说非常有用。
IE declaration
before the loop to initiate an instance of Internet Explorer; IE declaration
以启动Internet Explorer的实例; this is the only instance which will be used (the link is just going to be refreshed within this instance) set html = Nothing
within the loop set html = Nothing
循环中set html = Nothing
set ie = Nothing
outside of the loop, so that only the link may be refreshed without restarting IE set ie = Nothing
在循环之外set ie = Nothing
,因此只有链接可以刷新而不重新启动IE ie.Quit
only after parsing all >9000 webpages (so outside of the loop) ie.Quit
只有在解析了所有> 9000个网页之后(所以在循环之外) Hope it helps others with the same problem. 希望它能帮助其他人解决同样的问题。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.