2016-08-31 18 views
-2

我想在後臺打開IE下面,卻是花更多的時間來瀏覽後2〜3頁的導航,並在循環移動VBA打開IE作爲無形的不工作

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 

回答

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