VBA - Error: The 'frames' method of the 'JScriptTypeInfo' object failed

1

I need to fill in the box on the 'Legislation' tab with the text 'Energy Development Account' and do the research. But when executing the code it appears the error mentioned in the title referring to the line "obj = IE.doc [...]".

'Declara função Sleep
#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Sub CDE_ANEEL()

    Dim IE As Object

    Set IE = CreateObject("InternetExplorer.Application")

    IE.navigate "http://biblioteca.aneel.gov.br/index.html"
    IE.Visible = True

    Sleep 2000

obj = IE.document.frames(0).document.getEmelementByTagName("input")

i = 0

Do While i < obj.Length
    If obj(i).Name = "leg_campo1" Then
    obj(i).Value = "Conta de Desenvolvimento Energético"
    End If
Loop

End Sub

What should I do to fix this?

obs: Daniel, I made these changes. It fills in the box, but I can not click. Are there any errors in the code?

    Set doc = IE.document.getElementsByTagName("frame")(0).contentDocument.body
    Set doc1 = doc.getElementsByClassName("inputLegEsq")
    Set doc2 = doc.getElementsByClassName("button_busca")

For Each el In doc1        'Debug.Print el.Name, el.Value        If el.Name="leg_campo1" Then el.Value="Energy Development Account"        Next the

Sleep 2000

'Apertar Botão

For Each In doc2         Debug.Print.Title, el.onclick         Debug.Print InStr (1, el.onclick, "Check (5613,5, '', parent.hiddenFrame.modo_search)")         If InStr (1, el.onclick, "Check (5613,5, '', parent.hiddenFrame.modo_search)") > 0 Then el.Click     Next the

    
asked by anonymous 26.03.2018 / 21:01

1 answer

1

EDIT:

This code works the same as the other, but first click on the Legislation tab. I suggest using the code that generates txt with html to test this application. And learn about the DOM.

Sub CDE_ANEEL()

    Dim IE As Object
    Dim doc As Object, doc1 As Object, doc2 As Object, aba As Object
    Dim el

    Set IE = CreateObject("InternetExplorer.Application")

    IE.navigate "http://biblioteca.aneel.gov.br/index.html"
    IE.Visible = True
    EsperaIE IE, 2500
    Set doc = IE.document.getElementsbyTagName("frame")(0).contentdocument.body
    Set doc1 = doc.getElementsbyClassName("inputLegEsq")
    Set doc2 = doc.getElementsbyClassName("button_busca")
    Set aba = doc.getElementsbyClassName("text-aba")

    For Each el In aba
        'Debug.Print el.InnerText
        If el.InnerText = "Legislação" Then el.Click
    Next el

    For Each el In doc1
        'Debug.Print el.Name, el.Value
        If el.Name = "leg_campo1" Then
       el.Value = "Conta de Desenvolvimento Energético"
       el.InnerText = "Conta de Desenvolvimento Energético"
       el.Focus
        End If
    Next el

    'Aperta Enter
    Sleep 5000

    Application.SendKeys ("~"), True

End Sub

The use of the button.Click is not being possible because of the value onchange JavaScript code, which is not recognizing the change in the field until there is no interaction with the keyboard.

Original Response

Code

'Declara função Sleep
#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Sub CDE_ANEEL()

    Dim IE As Object
    Dim doc As Object, doc1 As Object, doc2 As Object
    Dim sFilename As String, sFilepath As String
    Dim objStream As Object
    Dim strData As String
    Dim el

    Set objStream = CreateObject("ADODB.Stream")
    sFilename = "temp.txt"
    sFilepath = ThisWorkbook.Path & "\" & sFilename

    Set IE = CreateObject("InternetExplorer.Application")

    IE.navigate "http://biblioteca.aneel.gov.br/index.html"
    IE.Visible = True
    EsperaIE IE, 2500
        Set doc = IE.document.getElementsbyTagName("frame")(0).contentdocument.body
        Set doc1 = doc.getElementsbyClassName("input_busca")
        Set doc2 = doc.getElementsbyClassName("button_busca")

    For Each el In doc1
        'Debug.Print el.Name, el.Value
        If el.Name = "rapida_campo" Then el.Value = "Conta de Desenvolvimento Energético"
    Next el

    'Aperta Enter
    Sleep 5000
    Application.SendKeys ("~"), True

'    'Apertar Botão
'    For Each el In doc2
''        Debug.Print el.Title, el.onclick
''        Debug.Print InStr(1, el.onclick, "Confere(5613,1,'','rapida')")
'        If InStr(1, el.onclick, "Confere(5613,1,'','rapida')") > 0 Then el.Click
'    Next el

'        'Cria arquivo txt com o HTML para Debug
'        Debug.Print IE.document.getElementsbyTagName("frame")(0).contentdocument.body.innerHTML
'
'        objStream.Type = 2                           'Specify stream type - we want To save text/string data.
'        objStream.Charset = "utf-8"                  'Specify charset For the source text data.
'        objStream.Open                               'Open the stream And write binary data To the object
'        objStream.WriteText doc.innerHTML
'        objStream.SaveToFile sFilepath, 2            'Save binary data To disk
'        'close down IE and reset status bar
'        objStream.Close
'    IE.Quit
'    Set IE = Nothing
End Sub
Public Sub EsperaIE(IE As Object, Optional time As Long = 250)
'Código de: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
    Sleep time
    Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.READYSTATE = 4) & _
                vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
    i = i + 1
Loop Until IE.READYSTATE = 4 Or Not IE.Busy
End Sub

Explanation

  

This code has a code below, that you generate a .txt file   with the HTML code, so you can test and learn more about DOM and   hierarchy of sites. Test in other applications ...

1 - Doc

First you find the iFrame and then the Body within this Frame with Set doc = IE.document.getElementsbyTagName("frame")(0).contentdocument.body

2 - Doc1

Then the Class input_search is assigned to doc1 Set doc1 = doc.getElementsbyClassName("input_busca")

3 - Doc2

Then the Class button_search is assigned to doc1 Set doc1 = doc.getElementsbyClassName("button_busca")

4 - Fill field

After the search field is filled, to find it, all the input_search class are found, but the one with the same name as the fast_field is filled in.

For Each el In doc1
    'Debug.Print el.Name, el.Value
    If el.Name = "rapida_campo" Then el.Value = "Conta de Desenvolvimento Energético"
Next el

5 - Press enter key

Hold for 5 seconds and press the Enter key. Therefore, you can not minimize or close the IE window. By clicking the button with the other code, an error occurred.

Sleep 5000
Application.SendKeys ("~"), True

More explanations:

More detailed explanations of the other functions have already been given this link

    
27.03.2018 / 15:19