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

Asked

Viewed 445 times

1

I need to fill the 'Legislation' tab with the text "Energy Development Account" and do the research. But when executing the code appears the error quoted 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 it?

BS: Daniel, I made these changes. It fills the box, but I can’t click. There’s some error 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 el

Sleep 2000

'Apertar Botão

For Each el In doc2 Debug.Print el.Title, el.onclick Debug.Print Instr(1, el.onclick, "Confer(5613,5,'',Parent.hiddenFrame.modo_search)") If Instr(1, el.onclick, "Confer(5613,5,'',Parent.hiddenFrame.modo_search)") > 0 Then el.Click Next el

1 answer

1


EDIT:

This code works the same way as the other code, but first click on the Legislation tab. I suggest using the code that generates txt with html to test this application. And learn about 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 button. Click is not possible because of the value onchange Javascript code, which is not recognizing the change in the field as long as there is no keyboard interaction.

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, which generates a file . txt with HTML code, for you to test and learn more about DOM and website hierarchy. Test on other applications...

1 - Doc

First you find the iframe and then Body inside that 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 - Fills field

After the search field is filled, to find it, all classes input_search are found, but the one that has the name equal to rapida_field is filled.

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

Wait 5 seconds and press the Enter key. So you cannot minimize or close the IE window. When clicking the button with the other code, an error occurred.

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

Further explanations:

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

  • Daniel, I did not specify in the question (I already reissued), but the text box is the 'Legislation' tab, because of that Enter does not work. I’ll try to change the code to tighten the other way you put it here

  • Daniel, which means "Confer(5613,3,'','rapida')") ?

  • 1

    You find several buttons with this code: <input name="bt_comb" tabindex="20" title="Submeter busca" class="button_busca" style="margin-bottom: 5px;" onclick="return Confere(5613,5,'',parent.hiddenFrame.modo_busca)" type="button" value="Buscar"> or <input name="bt_comb" title="Submeter busca" class="button_busca" style="margin-bottom: 5px;" onclick="return Confere(5613,3,'','rapida')" type="button" value="Buscar"> and this is the parameter inside the onclick quotes "".

  • I added the code change to the question. But I couldn’t click.

  • Daniel, I had changed the code and managed to change the text even without opening the tab, but another problem arose. I’m describing him in another question to not overload this. if you can take a look I thank you!

  • I couldn’t find a way to accomplish this without clicking on the tab. Because the use of button.Click is not possible because of the value onchange Javascript code, which is not recognizing the change in the field as long as there is no keyboard interaction.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.