Consulting

Results 1 to 5 of 5

Thread: VBA: Detect if the webpage on Internet Explorer is loaded

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Posts
    17
    Location

    VBA: Detect if the webpage on Internet Explorer is loaded

    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

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    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
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Feb 2018
    Posts
    17
    Location
    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

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    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
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Regular
    Joined
    Feb 2018
    Posts
    17
    Location
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •