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

用户名

我想用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窗口中打开了每个页面(而没有关闭前一个窗口)。要抓取9000多个网页,这很快就会成为一个很大的问题。

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

我想要的是IE应该在同一个标​​签页中打开每个网页(完成分析后,只需刷新已在使用中的标签页并加载下一页-或至少在完成分析后关闭窗口并打开下一个网页即可)在“新”窗口中)。可悲的是,直到现在我才设法找到解决方案。任何帮助,将不胜感激。

斯科特·霍尔兹曼

每次打开一个新窗口的原因是,您在此循环的开头将此行告诉它 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

excel vba 打印在 IE 浏览器中打开的文档

来自分类Dev

VBA-自动化错误-Excel 2013

来自分类Dev

VBA-自动化错误-Excel 2013

来自分类Dev

选项卡ColorIndex重置Excel VBA

来自分类Dev

自动化Excel VBA函数

来自分类Dev

循环浏览Excel Excel VBA

来自分类Dev

Access 2013 VBA自动使Excel丢失窗口

来自分类Dev

自动分组Excel VBA

来自分类Dev

Excel VBA自动筛选

来自分类Dev

VBA中的Excel RTD

来自分类Dev

Excel VBA中的超时

来自分类Dev

VBA Excel中的排列

来自分类Dev

在Excel VBA中循环

来自分类Dev

在Excel VBA中“包含”?

来自分类Dev

Excel VBA中的Vlookup

来自分类Dev

使用 Selenium + VBA 在浏览器中粘贴 Excel 范围

来自分类Dev

如何使用Excel VBA在PC和Mac中打开默认浏览器?

来自分类Dev

在 Excel (VBA) 中更改最后 4 个 Excel 工作表的选项卡颜色

来自分类Dev

从Access 2013运行Excel 2013宏时发生VBA自动化错误(系统调用失败)

来自分类Dev

从Access 2013运行Excel 2013宏时发生VBA自动化错误(系统调用失败)

来自分类Dev

VBA Excel中的VBA DateValue()中的错误?

来自分类Dev

Excel VBA打开各种Excel文件类型

来自分类Dev

打开 Excel 文档时禁用自动 VBA 窗口打开

来自分类Dev

由PHP生成的Excel VBA自动化的HTML表单

来自分类Dev

Excel 2013 VBA SelectionChange事件

来自分类Dev

自动文本大写Excel VBA

来自分类Dev

Excel VBA自动填充目标

来自分类Dev

Excel VBA 自动筛选代码

来自分类Dev

Excel VBA 自动填充问题