how do VBA interact with more than one tab in Internet Explorer?

Asked

Viewed 3,999 times

0

I have a macro in VBA that handles Internet Explorer, at certain point when clicking an item, a new tab opens in Internet Explorer.

My doubt is: how to change the focus of VBA to manipulate this new tab that has been opened?

follows code:

Sub x()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer
    ie.Visible = True
    ie.Navigate "http://www.comprasgovernamentais.gov.br/acesso-aos-sistemas/comprasnet-siasg"
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop

    Set objCollection = ie.Document.getElementsByTagName("a")
    i = 0
    n = objCollection.Length
    Do While i < n
        If objCollection(i).href = "https://www.comprasnet.gov.br/seguro/loginPortal.asp" Then
           objCollection(i).Click
           i = n
           Do Until ie.ReadyState = READYSTATE_COMPLETE
           Loop
           Sleep (3000)
        End If
        i = i + 1
    Loop

    ie.Document.getElementById("perfil").Item(2).Selected = True
    ie.Document.getElementById("perfil").fireEvent "onchange"
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop
    Sleep (3000)
    ie.Document.getElementById("txtLogin").Value = "xxxxxxx"
    ie.Document.getElementById("txtSenha").Value = "xxxxxxx"
    ie.Document.getElementById("acessar").Click
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop
    Sleep (3000)

    Set objCollection = ie.Document.frames(1).Document.getElementsByTagName("div")
    i = 0
    n = objCollection.Length
    Do While i < n
        If objCollection(i).innertext = "IRP" Then
            objCollection(i).fireEvent "onclick" 'após disparar este evento a nova aba é aberta
            i = n
            Do Until ie.ReadyState = READYSTATE_COMPLETE
            Loop
            Sleep (3000)
        End If
        i = i + 1
    Loop
End Sub

1 answer

0


Got it! the following code goes from tab to tab of IE and tests if the desired element is on the page and if the URL is equal to the desired

sub x()
     CapturaTela (irpNr)

end sub

Public Sub CapturaTela(irpNr As String)
    Dim url As String, NrSei As String
    Dim element As Object
    Dim cont As Integer
    cont = 1
    Set IE = Get_IE_Window2(irpNr)
End Sub

Private Function Get_IE_Window2(irpNr As String) As SHDocVw.InternetExplorer
    Dim Shell As Object
    Dim IE As Object
    Dim i As Variant
    Dim NrSei As Integer
    Set Shell = CreateObject("Shell.Application")
    i = 0
    Set Get_IE_Window2 = Nothing

    While i < Shell.Windows.Count And Get_IE_Window2 Is Nothing
        Set IE = Shell.Windows.Item(i) 'coloca o foco na janela "i" do IE
        If Not IE Is Nothing Then
            If InStr(IE.LocationURL, "https://www2.comprasnet.gov.br/siasgnet-irp") > 0 Then 'testa se a URL é a certa
                For Each element In IE.Document.getElementsBytagname("input")
                If element.name = "irp.numeroIrpFormatado" And element.Value = irpNr Then 'testa se o elemento desejado esta na pagina
                    NrSei = 100
                    Exit For 'achou a pagina procurada, sai do laço
                End If
                Next
            End If
            If Not IE Is Nothing Then
                Debug.Print IE.LocationURL, IE.LocationName
                If TypeName(IE) = "IWebBrowser2" Then 'verifica se é um webBrowser
                    If TypeOf IE Is SHDocVw.InternetExplorer And IE.LocationURL <> "" And NrSei > 0 Then 'testa se a url é nula, e se o elemento foi achado
                        Set Get_IE_Window2 = IE
                    End If
                End If
            End If
            i = i + 1
        Else: i = i + 1
        End If
    Wend
End Function

Browser other questions tagged

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