Excel VBA“对象'IWebBrowser2'的方法'文档'失败" [英] Excel VBA "Method 'Document' of object 'IWebBrowser2' failed"

查看:63
本文介绍了Excel VBA“对象'IWebBrowser2'的方法'文档'失败"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Excel 中自动提交表单以进行工作,但在基础知识方面有问题.我不断收到错误消息:

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:

对象'IWebBrowser2'的方法'文档'失败"

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

按原样使用代码,如果我在等待检查中包含 Or 部分,我会收到错误

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

我不知道在这里做什么,我到处寻找解决方案.此代码旨在最终做更多的事情,但它在第一次尝试 getElementsByTagName 时总是失败.

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

推荐答案

我不久前遇到了同样的问题.以中等完整性级别使用 Internet Explorer.InternetExplorer 默认为低完整性级别,如果您在工作中通过本地 Intranet 执行此操作,有时会出现上面显示的第二条错误消息.点击 此处 了解更多相关信息.我在下面修改了你的代码.如果有帮助,请告诉我.

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

记住 Microsoft Internet Controls 的参考资料,以及 Microsoft HTML Object Library 的参考资料,具体取决于您打算进一步做的事情

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

这篇关于Excel VBA“对象'IWebBrowser2'的方法'文档'失败"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆