Excel VBA to populate I.E. causes Run-time error in I.E. 11 but not 9

旧巷老猫 提交于 2019-12-13 02:06:06

问题


I created a sub to populate form fields in a website with data from an Excel workbook

The sub opens a new window in Internet Explorer, navigates to a intranet site, and populates form fields with data from an Excel workbook

This sub works using Win7 Excel 2010 and I.E. 9

When I use newer versions of I.E., the sub opens a new window in Internet Explorer and navigates to a intranet site, but fails to populate the form fields I receive the following error from VBA:

Run-time error ‘-2147417848 (80010108)’:

Automation error

The object invoked has disconnected from its clients

Here is my code

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

I've tried numerous changes to my code (below), but nothing works

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

Is there something about I.E. 10, 11, etc. that is different from I.E. 9?

Is there a way to force open an older version of I.E. (browser mode 9)

Any suggestions?


回答1:


For the script element, readyState is no longer supported. Starting with Internet Explorer 11, use onload.

IE 11 has different ways to access attributes:

Example: this will work for IE8 and 9

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

and this will work for IE11

Set doc = ie.document.frames

Set doc1 = doc.frames(2)


来源:https://stackoverflow.com/questions/34164966/excel-vba-to-populate-i-e-causes-run-time-error-in-i-e-11-but-not-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!