从vba网页下载每个文件(CSV / PDF)

时间:2018-09-19 19:10:42

标签: html excel-vba downloading

我需要帮助才能访问网页,下载并保存所有可用文件(CSV和PDF)。现在,我只能在网站上登录,然后单击正确的链接。现在,我需要帮助来下载和保存文件。

我现在的代码:

    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

我需要从中获取文件的页面:

enter image description here

0 个答案:

没有答案