PDA

View Full Version : [SOLVED] VBA: Detect if the webpage on Internet Explorer is loaded



baralus
04-11-2018, 04:15 AM
I just recently learn how to interact with Internet Explorer using VBA, and i have this code that I'm trying to get to work

Sub OpenNewTab()

Dim IE As Object
Dim site As String

With CreateObject("Shell.Application").Windows

If .Count > 0 Then
' Get IE
Set IE = .Item(0) ' or .Item(.Count - 1)
Else
' Create IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
End If
End With


URL = "https://www.automateexcel.com/excel/vba"

IE.navigate URL


Application.StatusBar = URL & " " & " is loading. Please wait..."

Do While IE.readyState = 4: DoEvents: Loop
Do Until IE.readyState = 4: DoEvents: Loop

Application.StatusBar = URL & " " & " Loaded"
Application.Wait (Now + TimeValue("0:00:02"))
Application.StatusBar = ""

Set IE = Nothing


End Sub

the problem is when i run it, the code goes to infinite loop because of the Do While. But I needed something like Do While to ensure that the webpage is already loaded before the code continues because I'm going to add another code that will do other things on the webpage when it is loaded. Hoping someone can help out. Thank you in advanced

mancubus
04-12-2018, 11:28 PM
try

1


Do While IE.Busy
DoEvents
Loop


2


Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop



alternate to 2


Do Until IE.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop



PS:
use READYSTATE_COMPLETE or 4

PPS:
1 + 2


Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop

baralus
04-12-2018, 11:47 PM
mancubus, i manage to make it work on the first run. When i run the macro for the first time and their is no active Internet Explorer, the macro opens a new Internet Explorer and the Do While loop is working which it waits until the webpage is load. But when i run the macro on the second time and the macro opens a webpage on a newtab of an active Internet Explorer, the Do While IE.Busy and Do While IE.readystate = 4 is false or no loop is happening making the code run continuously even when the webpage is no fully loaded. When i use Do While IE.readystate = 4 the loop is endless... need help on this problem

mancubus
04-13-2018, 04:10 AM
re-read my post pls and try:

Do Until IE.Busy Or IE.ReadyState = READYSTATE_COMPLETE
or
Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE

baralus
04-16-2018, 02:10 AM
I got it, my problem is not about the looping that will wait for the loading but the selection of what tab that need to be wait to load.



Sub GetIE()
Dim shellWins As ShellWindows
Dim IE As SHDocVw.InternetExplorer
Dim html As MSHTML.HTMLDocument
Dim htmlinput As MSHTML.IHTMLElement
Dim Shellfiles As Object

Set shellWins = New ShellWindows

If shellWins.Count > 1 Then
For Each Shellfiles In shellWins
b = b + 1
If Shellfiles.Name = "Internet Explorer" Then
Set IE = shellWins(b - 1)
NewTab = shellWins.Count
x = "google.com"
IE.Visible = True
IE.Navigate2 x, CLng(2048)
Application.Wait (Now + TimeValue("00:00:05"))
Exit For
End If
Next Shellfiles
Else
x = "google.com"
NewTab = shellWins.Count
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate x
End If


Set IE = shellWins(NewTab)

' Do While IE.Busy: DoEvents: Loop
' Do While IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: Loop
Do: Loop Until IE.ReadyState = READYSTATE_COMPLETE

Set html = IE.Document

Set htmlinput = html.getElementById("q")

htmlinput.Value = "Success"

Set shellWins = Nothing
Set IE = Nothing



End Sub