VBA [EXCEL 2013]:自动化:在同一浏览器中打开链接 Window / Tab
VBA [EXCEL 2013]: Automation: Open Links in Same Browser Window / Tab
我想用 VBA (Excel) 解析一长串本地网页(.HTM
个文件)并将一些数据提取到 excel。该程序需要抓取 9000 多个网页。这是一个例子:
> C:\Users\User_ID\Webpages\BS_1000.HTM.htm
C:\Users\User_ID\Webpages\BS_1001.HTM.htm
C:\Users\User_ID\Webpages\BS_1002.HTM.htm
C:\Users\User_ID\Webpages\BS_1003.HTM.htm
C:\Users\User_ID\Webpages\BS_1006.HTM.htm
C:\Users\User_ID\Webpages\BS_1007.HTM.htm
C:\Users\User_ID\Webpages\BS_1011.HTM.htm
C:\Users\User_ID\Webpages\BS_1012.HTM.htm
C:\Users\User_ID\Webpages\BS_1015.HTM.htm
C:\Users\User_ID\Webpages\BS_1016.HTM.htm
[... and the list goes on ...]
这里是 VBA:
<!-- language: lang-HTML -->
For startNumber = 1 To TotalProfiles
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles"
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_COMPLET
DoEvents
Loop
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
Set ie = Nothing
[some code here...]
问题是我当前的代码在新的 IE window 中打开每个页面(没有关闭前一个)。有超过 9000 个网页需要抓取,这很快就会成为一个非常大的问题。
我将 Internet Explorer 11(在 Windows 7 Enterprise SP1 上)与 Microsoft Office 2013 一起使用。
我想要的是 IE 应该在同一个选项卡中打开每个网页(几乎只是在完成解析后刷新已经 "in use" 的选项卡并加载下一页 - 或者至少关闭 window解析完成后打开下一个网页"new"window)。可悲的是,直到现在我才设法找到解决方案。任何帮助将不胜感激。
它每次打开一个新的 window 的原因是你在循环开始时用这一行告诉它 Set ie = New InternetExplorerMedium
有两种方法可以解决
- 循环前启动IE,循环结束后退出IE:
像这样:
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
For startNumber = 1 To TotalProfiles
Application.StatusBar = "Loading profile: " & ProfileNumber & "; file location: " & currentProfile
Do While ie.READYSTATE <> READYSTATE_COMPLET
DoEvents
Loop
Set currentProfile = Worksheets("List_of_Files").Range("B" & CurrentRowPosition)
ie.navigate currentProfile
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
[some code here...]
Next
Set html = Nothing
ie.Quit
Set ie = Nothing
- 每次结束循环之前退出 IE 实例(可能不如第一种方式有效)
像这样:
For startNumber = 1 To TotalProfiles
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles"
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_COMPLET
DoEvents
Loop
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
[some code here...]
Set html = Nothing
ie.Quit
Set ie = Nothing
Next
我想用 VBA (Excel) 解析一长串本地网页(.HTM
个文件)并将一些数据提取到 excel。该程序需要抓取 9000 多个网页。这是一个例子:
> C:\Users\User_ID\Webpages\BS_1000.HTM.htm
C:\Users\User_ID\Webpages\BS_1001.HTM.htm
C:\Users\User_ID\Webpages\BS_1002.HTM.htm
C:\Users\User_ID\Webpages\BS_1003.HTM.htm
C:\Users\User_ID\Webpages\BS_1006.HTM.htm
C:\Users\User_ID\Webpages\BS_1007.HTM.htm
C:\Users\User_ID\Webpages\BS_1011.HTM.htm
C:\Users\User_ID\Webpages\BS_1012.HTM.htm
C:\Users\User_ID\Webpages\BS_1015.HTM.htm
C:\Users\User_ID\Webpages\BS_1016.HTM.htm
[... and the list goes on ...]
这里是 VBA:
<!-- language: lang-HTML -->
For startNumber = 1 To TotalProfiles
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles"
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_COMPLET
DoEvents
Loop
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
Set ie = Nothing
[some code here...]
问题是我当前的代码在新的 IE window 中打开每个页面(没有关闭前一个)。有超过 9000 个网页需要抓取,这很快就会成为一个非常大的问题。
我将 Internet Explorer 11(在 Windows 7 Enterprise SP1 上)与 Microsoft Office 2013 一起使用。
我想要的是 IE 应该在同一个选项卡中打开每个网页(几乎只是在完成解析后刷新已经 "in use" 的选项卡并加载下一页 - 或者至少关闭 window解析完成后打开下一个网页"new"window)。可悲的是,直到现在我才设法找到解决方案。任何帮助将不胜感激。
它每次打开一个新的 window 的原因是你在循环开始时用这一行告诉它 Set ie = New InternetExplorerMedium
有两种方法可以解决
- 循环前启动IE,循环结束后退出IE:
像这样:
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
For startNumber = 1 To TotalProfiles
Application.StatusBar = "Loading profile: " & ProfileNumber & "; file location: " & currentProfile
Do While ie.READYSTATE <> READYSTATE_COMPLET
DoEvents
Loop
Set currentProfile = Worksheets("List_of_Files").Range("B" & CurrentRowPosition)
ie.navigate currentProfile
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
[some code here...]
Next
Set html = Nothing
ie.Quit
Set ie = Nothing
- 每次结束循环之前退出 IE 实例(可能不如第一种方式有效)
像这样:
For startNumber = 1 To TotalProfiles
Dim ie As InternetExplorerMedium
Set ie = New InternetExplorerMedium
ie.Visible = True
Application.StatusBar = "Loading profile " & ProfileNumber & " from a total of " & TotalProfiles & " profiles"
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_COMPLET
DoEvents
Loop
Application.StatusBar = "Storing " & currentProfile & " information into HTMLElement"
Set html = ie.document
[some code here...]
Set html = Nothing
ie.Quit
Set ie = Nothing
Next