0

I was trying to set up a public test environment to see if anyone would be able to help me with another question I asked this morning and I'm getting this error which I was not getting in my original code and after browsing a bit around I cannot fix: Automation error - The object invoked has disconnected from its clients.

Automation error - The object invoked has disconnected from its clients

Here is the full code:

Sub GetBranches()
    Dim objIE As InternetExplorer
    Set objIE = New InternetExplorerMedium ' create new browser

    objIE.Visible = True

    objIE.navigate "https://casadasereia.net/vbatests/viewtree241653.html"

    ' wait for browser
    Do While objIE.Busy = True Or objIE.readyState <> 4
        DoEvents
    Loop
End Sub

Anyone knows how to fix this?

Fabricio
  • 839
  • 9
  • 17
  • 1
    Try this --> change `Dim objIE As InternetExplorer Set objIE = New InternetExplorerMedium` to `Dim objIE as Object set objIE = CreateObject("InternetExplorer.Application")` – Santosh Jul 10 '18 at 10:52
  • Right! This worked. I have still to understand why and what is the difference between one method and the other to creating an object. But for now I'm content. Please create an answer with that and I'll accept it. – Fabricio Jul 10 '18 at 11:47
  • 1
    I'll just leave [this](https://stackoverflow.com/a/170084/5512705) here ;) Actually is a pretty elegant answer to your question, but I don't want to shamelessly rip it off and post it as my own – Samuel Hulla Jul 10 '18 at 14:08
  • Thanks @Rawrplus. The link to the MS Support site in that article is great: https://support.microsoft.com/en-us/help/245115/using-early-binding-and-late-binding-in-automation. – Fabricio Jul 11 '18 at 09:22

1 Answers1

0

Use late-binding to avoid additional reference to a library and this will fix versioning issues if any.

Sub GetBranches()
    Dim objIE as Object 
    Set objIE = CreateObject("InternetExplorer.Application")

    objIE.Visible = True

    objIE.navigate "https://casadasereia.net/vbatests/viewtree241653.html"

    ' wait for browser
    Do While objIE.Busy = True Or objIE.readyState <> 4
        DoEvents
    Loop
End Sub
Santosh
  • 12,175
  • 4
  • 41
  • 72