VBA Internet explorer Automation error - excel

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.
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?

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

Related

Filling web form fields but web page unable to detect text

I'm filling a web form using VBA, and I am able to fill text in the inputbox, but the webpage still is unable to detect the text and shows an error:
"Error: Required Field - Please provide an answer"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
URL = "https://npc.collegeboard.org/app/dartmouth/start"
objIE.Navigate URL
objIE.Document.getElementById("student.firstName").Focus
objIE.Document.getElementById("student.firstName").Value = "Tom"
Looks like theres some AngularJS running in the background, and it can't detect text fed in my VBA. Any help would be highly appreciated.
First of all after objIE.Navigate URL you should wait until the website is fully loaded and the IE is ready. This is done with the following:
objIE.Navigate URL 'this needs some time but VBA will continue excecuting the next statement qickly
Const READYSTATE_COMPLETE As Integer = 4
Do While objIE.Busy Or objIE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
'now IE is ready and the page is loaded.
But it might be that some JavaScript is not ready yet and this is not recognized by objIE.Busy Or objIE.ReadyState. So you can do a workaround:
Dim Obj As Object
Do While Obj Is Nothing
On Error Resume Next
Set Obj = objIE.Document.getElementById("student.firstName")
On Error GoTo 0
Loop
'now `student.firstName` is accessible, and probably all the other fields are too.
This will try to access the field student.firstName if it is not there it will error. We suppress the error message using On Error Resume Next and jump back to TryAgain until it was found.
Note that this has one disadvantage: If there is a problem loading this site it will get stuck in this loop. So I recommend to get a timed cancel criterium like if this takes more than a minute cancel it and throw a error message.
Something like the following should work:
Option Explicit
Sub test()
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
Dim URL As String
URL = "https://npc.collegeboard.org/app/dartmouth/start"
objIE.Navigate URL
Const READYSTATE_COMPLETE As Integer = 4
Do While objIE.Busy Or objIE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Dim Obj As Object
Do While Obj Is Nothing
On Error Resume Next
Set Obj = objIE.Document.getElementById("student.firstName")
On Error GoTo 0
Loop
objIE.Document.getElementById("student.firstName").Focus
objIE.Document.getElementById("student.firstName").Value = "Tom"
End Sub

VBA Internet Explorer - Object variables disappear

I am trying to learn web scraping with VBA and i am running into an issue in the most basic first step.
The problem (I think) is that after IEObject.Navigate is performed, all the variables of IEObject dissapear. So I get the error
"Run-time error '426': The remote server machine does not exist or is
unavailable"
on line 8. Any help is appreciated.
Code:
Sub VBAWeb()
Dim IEObject As InternetExplorer
Set IEObject = New InternetExplorer
IEObject.Visible = True
IEObject.Navigate URL:="https://google.com"
Do While IEObject.Busy = True Or IEObject.ReadyState <> READYSTATE_COMPLETE
Application.Wait Now + TimeValue("00:00:01")
Loop
Debug.Print IEObject.LocationURL
End Sub
I suggest you try to run code below on your side may help to fix the issue.
Sub demo()
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://www.microsoft.com"
IE.Navigate URL
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
Debug.Print IE.LocationURL
Set IE = Nothing
End Sub
You can see that the IE object was created differently. Also, the loop is a little bit different.

getElementById won't work in VBA, error 438

I am relatively new at VBA (I know the basics, but not much else) and I am trying to get some code set up that will fill out online forms for me, but when I run my code I get a 438 error:
object doesnt support this property or method
when it gets to
ie.document.getElementById ("q")
I have added the HTML object library and Microsoft internet controls to my references. I have looked at tons of online forums. I have even copied and pasted an entire script directly into VBA. Nothing will make it use getElementById(). Here is my code:
Sub internetstuff()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate ("https://www.google.com/")
Set searchbx = ie.document.getElementById("q")
searchbx.Value = "Howdy!"
End Sub
What should happen is that it should open InternetExplorer, go to Google, and populate the search bar with "Howdy!".
Instead, It only opens google, then I get the error message and it stops running.
Because "q" is not an ID, it's a Name.
You have to use GetElementsByName and select the 1st Element
try this:
Sub internetstuff()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.Navigate ("https://www.google.com/")
Do Until ie.ReadyState >= 4
DoEvents
Loop
Set searchbx = ie.document.getElementsByName("q")(0)
searchbx.Value = "Howdy!"
End Sub
I have also added a waiting Event, in case it takes time to load Google.
Result:
A more concise and efficient way is to use querySelector to return first match for the name attribute
Option Explicit
Public Sub internetstuff()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "https://www.google.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("[name=q]").Value = "Howdy"
Stop '<delete me later
.Quit
End With
End Sub

Scraping source code of website does not work on VDI

I have a problem with extracting data from the website using VBA on Citrix Virtual Desktop.
I have wrote my code on my local desktop first and it works good - HTML source has been extracted to the cell in Excel.
On VDI IE opens the website without any problems.
Code:
Sub GetBody()
Dim Body As String
the_start:
Set ObjIE = CreateObject("InternetExplorer.Application")
ObjIE.Visible = False
ObjIE.navigate ("https://pl.wikipedia.org/wiki/Wikipedia:Strona_g%C5%82%C3%B3wna")
Do
DoEvents
If Err.Number <> 0 Then
ObjIE.Quit
Set ObjIE = Nothing
GoTo the_start:
End If
Loop Until ObjIE.readyState = 4
Body = ObjIE.document.Body.innerHTML
Cells(1, 1).Value = Body
End Sub
When I try to run this code on VDI I am getting following error:
Run-time error '-2147467259(80004005)': Method 'Document' of object 'IWebBrowser2' failed.
Any ideas where this error comes from and what I should add to run it successfully on VDI?
I have done some changes mentioned in the comments (like changing the endless loop etc.) and also have another errors ( Automation error The object invoked has disconnected from its clients).. Previously I have declared IE as a object in this line below:
Set ObjIE = CreateObject("InternetExplorer.Application")
Soultion for all my problems:
Dim IE as SHDocVw.InternetExplorer
Set IE = New InternetExplorerMedium
Thank you all for participating in this thread and THANK YOU SO MUCH for your help!

Excel VBA force shutdown IE

I am currently using the following sub to close my IE after automating:
Public Sub CloseIE()
Dim Shell As Object
Dim IE As Object
Set Shell = CreateObject("Shell.Application")
For Each IE In Shell.Windows
If TypeName(IE.Document) = "HTMLDocument" Then
IE.Quit
End If
Next
End Sub
This works great but the problem occurs when I try to run the IE code again, I get the following:
Run-time error '-2147023706 (800704a6)':
Automation error
A system shutdown has already been scheduled.
After 20 secs, I can re-run the code. Is there any way of "force closing" IE so that I can run the code again directly after without the error?
EDIT:
Heres the code that initiates IE:
Sub testSub()
Dim IE As Object, Doc As Object, strCode As String
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.Navigate "website name"
Do While IE.ReadyState <> 4: DoEvents: Loop
Set Doc = CreateObject("htmlfile")
Set Doc = IE.Document
CODE HERE
CloseIE
End Sub
I used a simple procedure to create an InternetExplorer object, navigate, then close it using IE.Quit and Set IE = Nothing.
Apparently after closing, the internet was still running in the background for about another minute (I noticed it using task manager, background processes). I went into Internet Explorer options and unclicked “delete browsing history on exit”.
That fixed my issue. I am not sure why IE takes that long to clear the history and I’m not sure if there is a workaround but it is the only viable option for me thus far.
Modify your main sub to Quit the IE application, and then set the object variable to Nothing:
Sub testSub()
Dim IE As Object, Doc As Object, strCode As String
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.Navigate "website name"
Do While IE.ReadyState <> 4: DoEvents: Loop
Set Doc = CreateObject("htmlfile")
Set Doc = IE.Document
'### CODE HERE
IE.Quit
Set IE = Nothing
End Sub
You will not need the CloseIE procedure anymore.

Resources