Download PDF from URL in Excel VBA

reasem

New Member
Joined
Nov 15, 2019
Messages
38
looking to download a PDF from a URL. I can't seem to get past the login screen as the pdf that is downloaded only contains the code for the login page if I open it in NotePad. I examined the post request after logging in manually and pasted it after "FormData." I'm not sure if it matters what I called this variable? In one of the posts I reference below, he used "strAuthenticate."

When I examine the pdf that I want to download in chrome DevTools, it says this:

VBA Code:
<embed id="plugin" type="application/x-google-chrome-pdf"                                 
src="***SAME AS FILE URL IN VBA CODE***" 
stream-url="chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/761e8d06-3486-4bab-b043-df5b9a3c2510"     headers="accept-ranges: bytes
cache-control: max-age=1, must-revalidate
content-length: 375845
content-type: application/pdf
date: Wed, 06 May 2020 23:35:10 GMT
etag: 1588701484391
expires: Thu, 07 May 2020 00:35:10 GMT
last-modified: Tue, 05 May 2020 17:58:04 GMT
p3p: policyref=&quot;/w3c/p3p.xml&quot;, CP=&quot;NON DSP CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT&quot;
server: Microsoft-IIS/10.0
status: 200
x-content-type-options: nosniff
x-included-test: true
" background-color="0xFF525659" top-toolbar-height="56" javascript="allow" full-frame="">
Does it matter that the the pdf is a plugin rather than an attachment. Also the src is the site as what I have in the fileUrl in the below code.
VBA Code:
Sub SaveFileFromURL()
Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object

mainUrl = "https://www.website.com/j_security_check"
fileUrl = "https://www.website.com.com/controlFileRetrieve?ignorePresentViaObject=true&curDomId=111&posId=4574137"
filePath = "C:\myfile.pdf"

myuser = "xxxxxx"
mypass = "xxxxxx"

j_security_check = "j_username=" & myuser & "j_password=" & mypass

Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")


WHTTP.Open "POST", mainUrl, False 'WHTTP.Open "POST", fileUrl, False
WHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.send j_security_check


WHTTP.Open "GET", fileUrl, False
WHTTP.send

Debug.Print WHTTP.getAllResponseHeaders()

FileData = WHTTP.responseBody
Set WHTTP = Nothing

FileNum = FreeFile
Open filePath For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum

MsgBox "File has been saved!", vbInformation, "Success"

End Sub
Links that I have referenced or looked at:

VBA WinHTTP to download file from password proteced https website

How to make a POST request to a page that may redirect to a login page

Any help is appreciated!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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