首页 文章

Excel VBA 填充 I.E。导致 I.E 中的 Run-time 错误。 11 但不是 9

提问于
浏览
1

我创建了一个子程序,以使用 Excel 工作簿中的数据填充网站中的表单字段

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

该子项使用 Win7 Excel 2010 和 I.E 起作用。 9

当我使用较新版本的 I.E。时,该子项会在 Internet Explorer 中打开一个新窗口并导航到 Intranet 站点,但是无法填充表单字段,我从 VBA 收到以下错误消息:

Run-time 错误'-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

关于 I.E 的事情。与 I.E 不同的 10、11 等。 9?

有没有办法强制打开旧版本的 I.E。 (浏览器模式 9)

有什么建议么?

1 回答

  • 1

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

    IE 11 具有访问属性的不同方式:

    示例:这将适用于 IE8 和 9

    Set doc = ie.document
    Set doc1 = doc.frames(2).document 'here comes the error
    

    这将适用于 IE11

    Set doc = ie.document.frames
    
    Set doc1 = doc.frames(2)
    

相关问题