VBA :自动化:在同一浏览器窗口/选项卡中打开链接

我想用VBA(Excel)parsing一长串本地网页( .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窗口(不closures以前)。 随着超过9000个网页刮,这可能很快成为一个非常大的问题。

我在Microsoft Office 2013上使用Internet Explorer 11(在Windows 7 Enterprise SP1上)。

我想要的是,IE浏览器应该打开每个网页在同一个标​​签(几乎只是刷新已经在使用中的标签分析和加载下一页 – 或至lessclosures窗口完成parsing后,打开下一个网页在“新”窗口中)。 可悲的是,我直到现在才find解决办法。 任何帮助,将不胜感激。

它每次打开一个新窗口的原因是你用这一行在你的循环开始时告诉它Set ie = New InternetExplorerMedium

有两种方法来解决它。

  1. 在循环之前启动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 
  1. 每次在结束循环之前退出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