VBA打开IE作为隐形不起作用

时间:2016-08-31 06:24:50

标签: vba excel-vba word-vba excel

我正在尝试在后台打开IE,如下所示,但在2或3页导航后,它需要更多时间来导航并在循环中移动

For k= 2 to lr
check_w:
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate Sheets("sheet1").Range("E" & k).Value
ie.Visible = False
Application.Wait (Now() + TimeValue("00:00:03"))
check_webpage:
Set BDoc = Nothing
Web_app_Title = Sheets("sheet1").Range("E" & k).Value
search_WEB_APP_URL
If BDoc Is Nothing Then
    If ie Is Nothing Then
         Application.Wait (Now() + TimeValue("00:00:03"))
         GoTo check_w
    Else
        GoTo check_webpage
   End If
End If
next k

1 个答案:

答案 0 :(得分:0)

在循环的每次迭代中都会创建一个新的IE实例。 设置ie.visible = True进行测试或打开Windows任务管理器,你就会明白我的意思。

所以,很简单,在进入循环之前创建IE对象一次。完成循环后,在设置ie = Nothing之前退出。

Set ie = CreateObject("InternetExplorer.Application")

For k= 2 to lr
    ie.navigate Sheets("sheet1").Range("E" & k).Value
    ....
Next k

ie.quit
Set ie = nothing

如果您的IE对象遇到问题(无论出于何种原因),那么无论如何,您仍然可以包装

之类的内容
If ie = Nothing Then Set ie = CreateObject("InternetExplorer.Application")

在循环中。

提示:而不是Application.Wait (Now() + TimeValue("00:00:03"))我在Powershell中使用了While ie.busy = True之类的东西 - 不确定这是否也适用于VBA。

编辑:Do While ie.ReadyState <> 4 : Loop