I want to automate procedures on IE.
My macro works when opened on windows.
When I tried to run the same macro on tabs it didn't work.
It seems the macro isn't recognizing the tab.
Dim ie As Object
Dim dic As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
On Error Resume Next
ie.navigate "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/ConsultaPublicaAreasEmbargadas.php"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = ie.document
'IE.document.getElementById("num_cpf_cnpj").Value = "1234"
On Error Resume Next
doc.getElementById("num_cpf_cnpj").Value = Sheets("Main").Range("C10")
doc.getElementById("Emitir_Certificado").Click
' *until here works fine*
ie.Visible = True
ie.navigate "http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form", CLng(2048)
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = ie.document
' *this line doesn't work, the tab opens but nothing happens*
document.getElementById("num_cpf_cnpj").Value = Sheets("Main").Range("C10")
I tried to test your code on my side and found that after you open the second tab, your IE object still referring the first tab and inserting the value in it.
I try to modify your code added some code in it to find and move to the second tab and insert the value in that tab.
Sub demo()
Dim i As Long
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/ConsultaPublicaAreasEmbargadas.php"
IE.Navigate2 URL
Do While IE.readyState = 4: DoEvents: Loop 'Do While
Do Until IE.readyState = 4: DoEvents: Loop 'Do Until
IE.document.getElementById("num_cpf_cnpj").Value = "demo" 'Sheets("Main").Range("C10")
IE.document.getElementById("Emitir_Certificado").Click
IE.Navigate2 "http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form", 2048&
Application.Wait (Now + TimeValue("0:00:05"))
Set IE = GetIE("http://www.cnj.jus.br/improbidade_adm/consultar_requerido.php?validar=form")
IE.document.getElementById("num_cpf_cnpj").Value = "demo1"
'Unload IE
' Set IE = Nothing
' Set objElement = Nothing
' Set objCollection = Nothing
End Sub
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next 'because may not have a "document" property
'Check the URL and if it's the one you want then
' assign the window object to the return value and exit the loop
sURL = o.document.Location
On Error GoTo 0
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function
Output:
Further, you can try to modify the code example as per your requirement.