Excel VBA“对象'IWebBrowser2'的方法'文档'失败”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30086425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA "Method 'Document' of object 'IWebBrowser2' failed"
提问by James Beaulieu
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:
我正在尝试在 Excel 中自动提交表单以进行工作,但在基础知识方面遇到了麻烦。我不断收到错误消息:
"Method 'Document' of object 'IWebBrowser2' failed"
“对象‘IWebBrowser2’的方法‘文档’失败”
With the code as is, and if I include the Or part in the waiting check, I get the error
使用代码,如果我在等待检查中包含 Or 部分,我会收到错误
"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.
我不知道在这里做什么,我到处寻找解决方案。这段代码旨在最终做更多的事情,但它在第一次尝试时总是失败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
回答by Justin Schwebs
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 herefor more reading on this. I've modified your code below. Please let me know if that helps.
不久前我遇到了同样的问题。以中等完整性级别使用 Internet Explorer。InternetExplorer 默认为低完整性级别,如果您在工作中通过本地 Intranet 执行此操作,有时会出现上面显示的第二条错误消息。单击此处了解更多相关信息。我在下面修改了你的代码。如果这有帮助,请告诉我。
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
记住对 Microsoft Internet Controls 的引用,并且根据您计划进一步执行的操作,Microsoft HTML Object Library
回答by Reza Iranpour
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
只需将它放在要开始处理内容的行之前。要获取有关它如何工作的更多信息,您可以在此处查看
回答by Mangesh Vhatkar
The below method solved my problem for this error: Close all the explorer instances through 'Task manager' and try to run the code it will work.
以下方法解决了我针对此错误的问题:通过“任务管理器”关闭所有资源管理器实例并尝试运行它将起作用的代码。

