VBA Download pdf

5

I'm developing a macro in excel that through a list of ids and passwords in excel logs into a website and tries to download a pdf. I can log in to the site and navigate to the button that generates the pfd. The problem is that the button triggers a function in javascript that opens the pdf in a new window.

Button code

<td><input
class="tB" type="button" value="Emitir" onClick="javascript:obterFormulario()">
<br></td>

VBA to open pfd:

objIE.Navigate "javascript:obterFormulario('','')"

Thanks in advance to anyone who can help me.

    
asked by anonymous 26.05.2016 / 17:07

1 answer

0

When the new page opens, try changing the screen with the commands:

Parent_Window = driver.getWindowHandle
driver.switchTo.window(Parent_Window)

In this way you can access the new screen and interact with the PDF, for example by downloading it through the created URL, using the URLDownloadToFile function:

URL = objIE.LocationURL
Declare Function URLDownloadToFile _
    Lib "urlmon" Alias "URLDownloadToFileA" _
    (ByVal pCaller As Long, ByVal szURL As String, _
    ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    Function DownloadFile(URL As String, LocalFilename As String) As Boolean
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function
    Sub test()
        DownloadFile URL, "C:\PASTA\EXEMPLO.pdf"
    End Sub
    
29.10.2017 / 19:02