0

I need help to get to a web page, download and save all the avaiable files (CSV and PDF). For now I can only make login in the website and click on the right link. Now i need help to download and save the files.

My code for now:

    Sub login()

    Const Url$ = "https://www.hapvida.com.br/pls/webhap/webNewTrocaArquivo.login"

    Dim UserName As String, Password As String, LoginData As Worksheet
    Set LoginData = ThisWorkbook.Worksheets("1")

    UserName = LoginData.Cells(2, "B").Value
    Password = LoginData.Cells(3, "B").Value

    Dim ie As InternetExplorer

    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .Navigate Url
        ieBusy ie
        .Visible = True

        Dim oLogin As Object, oPassword As Object, oCodigo As Object


        Set oLogin = .Document.getElementsByName("pCpf")(0)
        Set oPassword = .Document.getElementsByName("pSenha")(0)

        oLogin.Value = UserName
        oPassword.Value = Password
        .Document.forms(0).submit

   ieBusy ie

   Set alllinks = ie.Document.getElementsByTagName("a")
   For Each Link In alllinks
   If Link.innerText = "BAIXAR ARQUIVOS - DOWNLOAD" Then
   Link.Click
   Exit For
   End If
   Next

    ieBusy ie


'Problematic part
   Set fulllinks = ie.Document.getElementsByTagName("a")
   For Each Links In fulllinks
   If Right(Links.innerText, 3) = "PDF" Then
   Links.Click
   Exit For
   End If
   Next

     ieBusy ie

     End With

End Sub


Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.ReadyState < 4
        DoEvents
    Loop
End Sub

The page i need to get the files from:

enter image description here

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Does using `Links.Click` bring up a download dialog for each link? I think your best bet is to create a list of all links, then use some other method to download them. It might be difficult depending on how the security/login works on that site though. – NickSlash Sep 19 '18 at 19:27
  • It´s not feasible because the documents for download are not the same... they are upgraded periocally by the company. So what i need is a routine to go to that page and download every available files. – Felipe Soares Sep 19 '18 at 20:57
  • And no, it does not open any dialog. the code just stops there. May it be because there are several links that attends the criteria? – Felipe Soares Sep 19 '18 at 21:04
  • I was suggesting you create a list every time to pass off to something to handle the downloading. https://stackoverflow.com/a/22113944/212869 for example, you would need to figure out how the login is handled to use it though. – NickSlash Sep 20 '18 at 09:58

0 Answers0