2017-02-17 145 views
2

我想使用VB腳本在IE上打開幾個不同的網站,他們將每隔10秒刷新所有網頁。但現在我有這個代碼,它只能刷新第一個網頁,第二個網頁根本不刷新。你能幫忙嗎?非常感謝。如何使用VB腳本刷新IE上的所有網頁?

Set objExplorer = CreateObject("InternetExplorer.Application") 
WebSite ="http://keono.com/" 
with objExplorer 
.Navigate2 WebSite 
.AddressBar = 1 
.Visible = 1 
.ToolBar = 1 
.StatusBar = 1 
end with 

WebSite = "http://enquotemarketing.com/" 
with objExplorer 
.Navigate2 WebSite, &h800 
.AddressBar = 1 
.Visible = 1 
.ToolBar = 1 
.StatusBar = 1 
end with 


Do While True 
WScript.Sleep 10000 ' 10 seconds 
objExplorer.Refresh() 
Loop 

回答

1

我的理解是你試圖在不同的IE選項卡中打開網頁,然後刷新它們。通過.Navigate2 URL, &h800打開的新選項卡是在單獨的全新IE實例中創建的,但由於新選項卡屬於IE窗口,因此它具有與IE窗口相同的HWND。因此,在導航之後,您可以檢查具有相同HWND的每個資源管理器窗口並將新創建的窗口存儲到數組中。然後刷新其中的每個IE窗口。下面是示例代碼:

Option Explicit 

Dim aTabs, aURLs, oIE, lIEHwnd, i, j, oWnd 

' Navigate URLs in tabs 
aURLs = Array(_ 
    "http://keono.com/", _ 
    "http://enquotemarketing.com/", _ 
    "http://stackoverflow.com/" _ 
) 
ReDim aTabs(UBound(aURLs)) 
Set oIE = CreateObject("InternetExplorer.Application") 
oIE.Visible = True 
lIEHwnd = oIE.Hwnd ' IE window HWND 
For i = 0 To UBound(aURLs) 
    oIE.Navigate2 aURLs(i), &H800 ' Open the page in another IE instance 
    ' Process until new IE instance is created 
    Do 
     j = 0 
     For Each oWnd In CreateObject("Shell.Application").Windows 
      If oWnd.Hwnd = lIEHwnd And Not (oWnd Is oIE) Then 
       j = j + 1 
       Set aTabs(i) = oWnd ' Assuming the last window 
      End If 
     Next 
    Loop Until j = i + 1 
Next 
oIE.Quit ' Close 1st empty tab 
' Wait until all tabs are ready 
For Each oIE In aTabs 
    Do While oIE.ReadyState < 3 Or oIE.Busy 
     WScript.Sleep 10 
    Loop 
Next 
' Refresh tabs 
Do 
    WScript.Sleep 10000 
    For Each oIE In aTabs 
     oIE.Refresh 
    Next 
Loop 
+0

偉大的答案,不知道爲什麼VBA示例是相關的,但。這個問題是關於VBScript和添加VBA示例只是混淆了答案。 – Lankymart

+0

@Lankymart沒錯,刪除了VBA,無論如何它可以在修訂歷史中找到。 – omegastripes