VBA Code to Download All PDF Links on Webpage

angusfire

New Member
Joined
Feb 24, 2012
Messages
34
I am needing to download all the PDFs from a webpage and save them into a folder. The PDFs on the webpage are downloaded via different links. Here is the webpage that the PDFs are located: NRCS Engineering Manuals and Handbooks | NRCS North Dakota. I have already created the folder location as shown below:
Code:
Function FileFolderExists(strFullPath As String) As Boolean
'Macro Purpose: Check if a folder exists
    On Error GoTo EarlyExit
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
    
EarlyExit:
    On Error GoTo 0
End Function

'Creates file folder for saving imported precipitation data
'Used as Micro for "Make Destination Folder" Button

Sub Make_Folder()
    Dim objFSO As Object
    Dim objFolder As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
     
     'Get the folder object associated with the directory
    Set objFolder = objFSO.GetFolder(CurDir())
    
    Range("u11").Select
    Selection.ClearContents
    
' Opens windows explorer for creation of folder to save .pdf files
If Len(Dir(CurDir() & "\Stockwater PDFs", vbDirectory)) = 0 Then MkDir (CurDir() & "\Stockwater PDFs")

    If FileFolderExists(CurDir() & "\Stockwater PDFs") Then
        MsgBox "Folder Created Sucessfully!!!"
    Else
        MsgBox "Folder does not exist!"
    End If
    
    If FileFolderExists(CurDir() & "\Stockwater PDFs") Then
    ActiveSheet.Range("u11").Value = "Stockwater PDFs folder made in the " & objFolder.Name
    End If
    
End Sub

Once the files are downloaded from the website (the first part of the code below that I need help with) I would then list what files are located within the folder they were saved into (which I have already):

Code:
Sub GetWebPageDocs()

' Erases all listed files shown to be located in the CurDir()\Stockwater PDFs folder

    Range("n17:n50").Select
    Selection.ClearContents
    Range("n16").Select

' Lists current files located in the CurDir()\Stockwater PDFs folder

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
     
     'Get the folder object associated with the directory
    Set objFolder = objFSO.GetFolder(CurDir() & "\Stockwater PDFs")
    irow = 17
    icolumn = 14
    ActiveSheet.Range("N16").Value = "The files found in the " & objFolder.Name & " folder are:"

     'Loop through the Files collection
    For Each objFile In objFolder.Files
        ActiveSheet.Cells(irow, icolumn).Value = objFile.Name
        irow = irow + 1
        icolumn = icolumn
    
    Next
     
     'Clean up!
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing

End Sub

If it would be easier to have all the desired PDFs located within a ZIP file that was a single link on the website then that may be the best option.

Thanks in advance for the help.
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Watch MrExcel Video

Forum statistics

Threads
1,123,328
Messages
5,600,982
Members
414,418
Latest member
mightyMagnus

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
Top