VBA Download pdf

Asked

Viewed 901 times

6

I am developing a macro in excel that through a list of id and passwords in excel logs into a website and tries to download a pdf. I can log into the site and navigate to the button that generates pfd. The problem is that the button triggers a javascript function 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('','')"

From now on I thank anyone who can help me.

  • 1

    Hello. Welcome to SOPT. It’s hard to help without more details. When you say that the problem is that it opens in a new window, what exactly is your difficulty? Can’t you access the window? Also did not understand the code snippet in VBA. What you expect it to do?

  • 1

    Well, for lack of detail, I may have misunderstood what your problem is. But if what you want is to trigger the event that is on the button , search by the method .FireEvent("event")

  • Behold this answer where you probably need to trigger an event.

1 answer

0

When the new page opens, try switching from screen to commands:

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

This way you can access the new screen and interact with the PDF, for example by downloading it from 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

Browser other questions tagged

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