Download PDF generated with Javascript

Tegenfeldt

New Member
Joined
Aug 6, 2013
Messages
13
I have an Excel VBA that opens a webpage and clicks a link. The link runs a JavaScript that generates a PDF page in a new IE window. So far all is fine, but I have not found out how to save (download) the PDF. Below is my VBA, test it and you can see what I mean:

Rich (BB code):
Sub GeneratedPDF()    Dim IExplorer As InternetExplorer
    Dim TheURL As String
    Dim HTMLdoc As HTMLDocument
    Dim TheLink As HTMLLinkElement
 
    TheURL = "core.purus.se/PurusWeb/Navigate/Document.aspx?DBDocId=13284"
    Set IExplorer = New InternetExplorer
    With IExplorer
        .navigate TheURL
        .Visible = True
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        Set HTMLdoc = .Document
        Set links = HTMLdoc.getElementsByTagName("a")
        i = 0
        Set TheLink = Nothing
        'And HTMLdoc.links(i).className = "ddmenu"
        Do While i < HTMLdoc.links.Length
            If HTMLdoc.links(i).className = "PDFLink" Then
                Set TheLink = HTMLdoc.links(i)
                Debug.Print TheLink
                Exit Do
            End If
            i = i + 1
        Loop
    End With
    TheLink.Focus
    TheLink.Click
    IExplorer.Quit
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This isn't a full solution but perhaps it will help.

I was able to download the PDF with this code.
Code:
Option Explicit

Private 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

Sub DownloadFilesFromWeb()
Dim strURL As String
Dim strFileName As String
Dim strSavePath As String
Dim returnValue As Long



    strURL = "http://core.purus.se/PurusWeb/Navigate/PdfQueue.aspx?&FORCE=9720&Add=Product&Catalog=Product&DocId=1&Style=SimplePdf&Hires=&Level=&Language="

    strFileName = "MiniBrage50.pdf"

    strSavePath = "C:\test\" & strFileName

    returnValue = URLDownloadToFile(0, strURL, strSavePath, 0, 0)

    If returnValue = 0 Then
        Debug.Print "Success!"
    Else
        Debug.Print "Did not succeed."
    End If


End Sub

I got the address for the file from the source code of the window that pop-ups.

See the src attribute of the frame here:
HTML:
<head>
		<title>PDF-dokument</title>
	</head>
	<frameset rows="*">
		<frame name="Main" src="PdfQueue.aspx?&FORCE=9720&Add=Product&Catalog=Product&DocId=1&Style=SimplePdf&Hires=&Level=&Language=">
	</frameset>

You should be able to add to your code to retrieve the address and then use the download code to retrieve the document.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top