Macro that downloads pdf file to designated folder from URL.

This example shows how to download a pdf from a URL to your computer.  Its very useful when you have a large number of pdf’s you need to download from the internet.

Update variable strPDFLink to the URL of the pdf file.

strPDFLink = ""

Update variable strPDFFile to change the name of the file.

strPDFFile = strDir & "\DownloadPDF_fromURL_" & Format(Now, "") & ".pdf"
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

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 DownloadPDF()
Dim strPDFLink As String
Dim strPDFFile As String
Dim doc, hcol, text As Variant
Dim ie As SHDocVw.InternetExplorer
Set ie = CreateObject("InternetExplorer.Application")

   MsgBox "Select Folder to Save .pdf to)"
Dim FolderName As String
With Application.FileDialog(msoFileDialogFolderPicker)
   .AllowMultiSelect = False

   On Error Resume Next
   strDir = .SelectedItems(1)
   On Error GoTo 0
 End With

    strPDFLink = ""
    strPDFFile = strDir & "\DownloadPDF_fromURL_" & Format(Now, "") & ".pdf"
    ie.Visible = True
    ie.Navigate (strPDFLink)
    Application.Wait (Now + #12:00:02 AM#)
    Result = DownloadFile(strPDFLink, strPDFFile)

End Sub

Interested in learning new skills?