EDIT:
Este código funciona do mesmo jeito que o outro, mas antes clica na aba Legislação. Sugiro utilizar o código que gera o txt com o html para testar esta aplicação. E aprender sobre o 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
A utilização de button.Click não está sendo possível por causa do código JavaScript de value onchange, que não está reconhecendo a alteração no campo enquanto não houver interação com o teclado.
Resposta Original
Código
'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
Explicação
Este código possui um código em baixo, que você gera um arquivo .txt
com o código HTML, para você testar e aprender mais sobre DOM e
hierarquia de sites. Teste em outras aplicações...
1 - Doc
Primeiro você encontra a iFrame e depois o Body dentro dessa Frame com Set doc = IE.document.getElementsbyTagName("frame")(0).contentdocument.body
2 - Doc1
Depois a Class input_busca é atribuída a doc1 Set doc1 = doc.getElementsbyClassName("input_busca")
3 - Doc2
Depois a Class button_busca é atribuída a doc1 Set doc1 = doc.getElementsbyClassName("button_busca")
4 - Preenche campo
Depois o campo de busca é preenchido, para encontrá-lo, todas as class input_busca são encontradas, mas a que possui o nome igual a rapida_campo é preenchida.
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 - Aperta tecla enter
Espera 5 segundos e aperta a tecla Enter. Portanto, não pode minimizar ou fechar a janela do IE.
Ao clicar no botão com o outro código, um erro ocorria.
Sleep 5000
Application.SendKeys ("~"), True
Mais explicações:
As explicações mais detalhadas das outras funções já foram dadas neste link