Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA "Method 'Document' of object 'IWebBrowser2' failed"

I'm trying to automate a form submission in Excel for work, and In have trouble with the basics. I keep getting the error message:

"Method 'Document' of object 'IWebBrowser2' failed"

With the code as is, and if I include the Or part in the waiting check, I get the error

"Automation Error The object invoked has disconnected from its clients."

I'm not sure what to do here, I've searched all over for solutions. This code is intended to eventually do more than this, but it keeps failing on the first try to getElementsByTagName.

Sub GoToWebsiteTest()
Dim appIE As Object 'Internet Explorer
Set appIE = Nothing
Dim objElement As Object
Dim objCollection As Object

If appIE Is Nothing Then Set appIE = CreateObject("InternetExplorer.Application")
sURL = *link*
With appIE
    .Visible = True
    .Navigate sURL
End With

Do While appIE.Busy ' Or appIE.ReadyState <> 4
    DoEvents
Loop

Set objCollection = appIE.Document.getElementsByTagName("input")

Set appIE = Nothing
End Sub
like image 454
James Beaulieu Avatar asked May 06 '15 20:05

James Beaulieu


2 Answers

I ran into this same issue a while back. Use internet explorer at a medium integrity level. InternetExplorer defaults to a low integrity level which, if you are doing this over a local intranet at work, sometimes will give the second error message you show above. Click here for more reading on this. I've modified your code below. Please let me know if that helps.

Sub GoToWebsiteTest()
Dim appIE As InternetExplorerMedium
'Set appIE = Nothing
Dim objElement As Object
Dim objCollection As Object

Set appIE = New InternetExplorerMedium
sURL = "http://example.com"
With appIE
    .Navigate sURL
    .Visible = True
End With

Do While appIE.Busy Or appIE.ReadyState <> 4
    DoEvents
Loop

Set objCollection = appIE.Document.getElementsByTagName("input")

Set appIE = Nothing
End Sub

Remember references for Microsoft Internet Controls, and depending on what you plan on doing further, Microsoft HTML Object Library

like image 128
Justin Schwebs Avatar answered Oct 16 '22 01:10

Justin Schwebs


Not exactly same as above code but somehow similar , the following code solved my problem:

Do
Loop Until ie.readystate = 3
Do
Loop Until ie.readystate = 4

Just put it before the line you want to start working with the contents. To get more information about how does it work you can check here

like image 36
Reza Iranpour Avatar answered Oct 16 '22 02:10

Reza Iranpour