Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...