Automatically Download PDF Links to Desktop Folder

Hoosiers18

New Member
Joined
Jan 8, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have code that currently loops through a list of hyperlinks in Excel and batch downloads these links as PDFs. I am having trouble controlling where these downloads ave. Sometimes they save to my desktop, documents, or another file path. Ideally, I would like them to be saved into a designated folder on my desktop named "PDFs." Below is the code I currently have:


Declare PtrSafe 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 DownloadPDFs()
Dim StartRowNum As Long
Dim EndRowNum As Long
Dim pdfname As String
Dim RecordNum As String
Dim URLprefix As String
LastRowPDF = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
For i = 2 To LastRowPDF
Application.ScreenUpdating = False
URLprefix = Sheet1.Cells(i, 2)
RecordNum = Sheet1.Cells(i, 3)
pdfname = RecordNum & ".pdf"
URL = URLprefix
DownloadFile URLprefix, pdfname
Application.ScreenUpdating = True

Next i

End Sub


If anyone knows how to make this code save into a specific folder path, that would be greatly appreciated! Thank you.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Welcome

An example:



VBA Code:
Declare PtrSafe Function URLDownloadToFile _
Lib "urlmon" Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName$, ByVal dwReserved&, ByVal lpfnCB As Long) As Long

Function DownloadFile(URL$, LocalFilename As String) As Boolean
Dim lngRetVal&
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function

Sub DownloadXL()
Dim StartRowNum As Long, i%, sh As Worksheet, filepath$
Set sh = ActiveSheet
For i = 2 To sh.Cells(Rows.Count, "B").End(xlUp).row
    With WorksheetFunction
        filepath = "c:\test\" & Mid(sh.Cells(i, 2), .Find("*", _
        .Substitute(sh.Cells(i, 2), "/", "*", Len(sh.Cells(i, 2)) - _
        Len(.Substitute(sh.Cells(i, 2), "/", "")))) + 1, Len(sh.Cells(i, 2)))
    End With
    sh.Cells(i, 3) = filepath
    If DownloadFile(sh.Cells(i, 2), sh.Cells(i, 3)) Then MsgBox "Success!", , i
Next
End Sub
 
Upvote 0
I would like them to be saved into a designated folder on my desktop named "PDFs."
Insert before the loop:
VBA Code:
    Dim folder As String
    folder = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\PDFs\"
And replace:
VBA Code:
DownloadFile URLprefix, pdfname
with:
VBA Code:
    DownloadFile URLprefix, folder & pdfname
PS please use VBA tags.
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,481
Members
452,516
Latest member
archcalx

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