How to refresh all webpages on IE by using VBscrip

2019-07-07 17:05发布

问题:

I'm trying to use vb script to open several different websites on IE and they will refresh all web pages in every 10 seconds. but now i have this codes, it only can refresh the first web, the second web doesn't refresh at all. Could you help with this? Thanks a lot.

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:

My understanding is you are trying to open webpages in different IE tabs and then refresh them. New tab opened via .Navigate2 URL, &h800 is created in a separate brand new IE instance, but since new tab belongs to the IE window, it has the same HWND as IE window. Thus after navigate you can check each explorer window having the same HWND and store new created window to array. Then refresh each IE window within it. Here is the example code:

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