Save PDF from URL in Excel

ForrestGump01

New Member
Joined
Mar 15, 2019
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hello all,
It's been a long time since I've used any VBA and it's not coming back like riding a bicycle... Hoping for some help on hopefully easy task.

I have a sheet with tens of thousands of rows of invoice data -- each row is a discrete invoice with various columns of information, including a column which includes a URL to a .pdf copy of the invoice. I would like to write a macro that opens the pdf, downloads it, and saves it using a reference to a row/column.

Example: Row 2 is an invoice for XYZCompany Invoice #123456 -- I would like the script to save the file to C:\Desktop\Invoices as "XYZCompany_#123456" and then proceed to the next line of data. I already have a column to concatenate the future file name, I just could use some help with the VBA.

Thanks in advance!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
No problem.

VBA Code:
#If VBA7 And Win64 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As Long
#Else
    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
#End If

Function Download(ByVal URL As String, ByVal Destination As String) As Long
    Download = URLDownloadToFile(0, URL, Destination, 0, 0)
End Function

This needs to go in its own module, or at the top of one, at least. Then you can call it with Download PDFFilename, DestinationFilename

Does that help?
 
Upvote 0
No problem.

VBA Code:
#If VBA7 And Win64 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As Long
#Else
    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
#End If

Function Download(ByVal URL As String, ByVal Destination As String) As Long
    Download = URLDownloadToFile(0, URL, Destination, 0, 0)
End Function

This needs to go in its own module, or at the top of one, at least. Then you can call it with Download PDFFilename, DestinationFilename

Does that help?

Thank you!

When you say "Call it" you mean set the Sub declarations?

I've just reorg'd my data. Column A are all the hyperlinks, Column B is the "save as file name". What's the loop I should write to run your recommended code for all the instances in these two columns and save to C:\Desktop\Invoices?
 
Upvote 0
Thank you!

When you say "Call it" you mean set the Sub declarations?

I've just reorg'd my data. Column A are all the hyperlinks, Column B is the "save as file name". What's the loop I should write to run your recommended code for all the instances in these two columns and save to C:\Desktop\Invoices?
I am so very sorry - I am certain that I responded to this because I remember writing the a short piece of code for you because it was straight forward. I can't apologise enough/ Were you able to work it out? Do you need any help with it now?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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