Excel VBA填充IE导致在IE 11运行时错误,但不是9

我使用Excel工作簿中的数据创build了一个子项,以便在网站中填充表单域

该子在Internet Explorer中打开一个新窗口,导航到Intranet站点,并使用Excel工作簿中的数据填充表单域

本子使用Win7 Excel 2010和IE 9

当我使用较新版本的IE浏览器时,在Internet Explorer中打开一个新窗口并导航到Intranet站点,但无法填充表单字段,从VBA收到以下错误:

运行时错误“-2147417848(80010108)”:

自动化错误

被调用的对象已经与客户端断开连接

这是我的代码

Sub hOpenWsAndPopulate() Dim ie As Object Dim frm As Variant Dim element As Variant Set ie = CreateObject("InternetExplorer.Application") ie.navigate "www.mywebsite.com" While ie.readyState <> 4: DoEvents: Wend ' try to get form by ID Set frm = ie.document.getElementById("incidentTicket_Form") ' try to get form by tag name and index. ' Item(0) = 1st form ' Item(1) = 2nd form ' Item(2) = 3rd form, etc. If frm Is Nothing Then Set frm = ie.document.getElementsByTagName("form").Item(0) If frm Is Nothing Then MsgBox "Form not found" ie.Quit Set ie = Nothing Else ie.Visible = True For Each element In frm.elements On Error Resume Next Select Case element.Name ' Input Classification Case "strClassificationName": element.Value = Sheets("OUT-Class").Range("Classification") ' Input Short Description Case "txtShortDescription": element.Value = Sheets("OUT-SD").Range("SD_Output") ' Input Long Description Case "txtLongDescription": element.Value = Sheets("OUT-LD").Range("LD_Output") ' Input CC Case "strCCString": element.Value = Sheets("OUT-CC").Range("CcBa") End Select Next End If End Sub 

我已经对我的代码(下面)进行了大量更改,但没有任何效果

 Sub zzzOpenWsAndPopulate() Dim objIE As InternetExplorer Dim htmlDoc As HTMLDocument Dim htmlFrame As HTMLFrameElement Dim frame As HTMLIFrame Dim htmlElement As HTMLDTElement Dim myDoc As Object Dim frm As Variant Dim element As Variant ' Set the variables Set objIE = New InternetExplorer ' Make the browser visible and navigate With objIE .Visible = True .navigate "www.mywebsite.com" End With hWaitForInternetToLoad objIE Set frm = objIE.document.getElementById("incidentTicket_Form") If frm Is Nothing Then Set frm = ie.document.getElementsByTagName("form").Item(0) If frm Is Nothing Then MsgBox "Form not found" ie.Quit Set ie = Nothing Else objIE.Visible = True For Each element In frm.elements On Error Resume Next Select Case element.Name ' Input Classification Case "strClassificationName": element.Value = Sheets("OUT-Class").Range("Classification") ' Input Short Description Case "txtShortDescription": element.Value = Sheets("OUT-SD").Range("SD_Output") ' Input Long Description Case "txtLongDescription": element.Value = Sheets("OUT-LD").Range("LD_Output") ' Input CC Case "strCCString": element.Value = Sheets("OUT-CC").Range("CcBa") End Select Next End If End Sub 

有什么关于IE 10,11等,不同于IE 9?

有没有办法强制打开旧版IE浏览器(浏览器模式9)

有什么build议么?

对于脚本元素,readyState不再受支持。 从Internet Explorer 11开始,使用onload

IE 11有不同的访问属性的方法

例如:这将适用于IE8和9设置doc = ie.document设置doc1 = doc.frames(2).document'这里出现错误

这将适用于IE11 Set doc = ie.document.frames

设置doc1 = doc.frames(2)