Macro to download files from the internet

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
I have this macro, which I want to download all the files given by the hyperlinks in column a. The problem is that the line of code with the Choose function isn't working. Is there a way to enter an array as the value argument in Choose or another function that will let me do what Choose tries to do?

Thanks!

Code:
Sub DownloadFiles()
' Downloads files given by any hyperlinks in Column A.
    Const strPath As String = "G:\DownloadData\"
    Dim strFile As String, lnum As Long
    Dim rng As Range
    Set rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
     
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    For lnum = 1 To rng.Count
        strFile = Choose(lnum, rng)
        ThisWorkbook.FollowHyperlink strFile
        With ActiveWorkbook
            .SaveAs strPath & strFile
            .Close
        End With
    Next lnum
     
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this.
Code:
strfile = Range("A"& lnum).Value
 
Upvote 0
Thanks for the suggestion, but it didn't work. I should have mentioned that the cells only have the filename as the cell value not the link. The following code works, but it saves the file as an excel workbook that when opened opens the internet file.

Is there a way to save the files it opens as pdfs in the specified folder?

Thanks!

Code:
[FONT=Courier New]Sub DownloadFiles()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT]
[FONT=Courier New][/FONT] 
[FONT=Courier New]    Const strPath As String = "G:\DownloadData\"<o:p></o:p>[/FONT]
[FONT=Courier New]    Dim strFile As String, lnum As Long<o:p></o:p>[/FONT]
[FONT=Courier New]    Dim rng As Range<o:p></o:p>[/FONT]
[FONT=Courier New]    Set rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)<o:p></o:p>[/FONT]
[FONT=Courier New]     <o:p></o:p>[/FONT]
[FONT=Courier New]    Application.ScreenUpdating = False<o:p></o:p>[/FONT]
[FONT=Courier New]    Application.DisplayAlerts = False<o:p></o:p>[/FONT]
[FONT=Courier New]    <o:p></o:p>[/FONT]
[FONT=Courier New]    For Each cell In rng<o:p></o:p>[/FONT]
[FONT=Courier New]        strFile = "web address" + cell.Value + ".pdf"<o:p></o:p>[/FONT]
[FONT=Courier New]        ThisWorkbook.FollowHyperlink strFile<o:p></o:p>[/FONT]
[FONT=Courier New]        With ActiveWorkbook<o:p></o:p>[/FONT]
[FONT=Courier New]            .SaveAs strPath & cell.Value<o:p></o:p>[/FONT]
[FONT=Courier New]        End With<o:p></o:p>[/FONT]
[FONT=Courier New]    Next cell<o:p></o:p>[/FONT]
[FONT=Courier New]     <o:p></o:p>[/FONT]
[FONT=Courier New]    Application.ScreenUpdating = True<o:p></o:p>[/FONT]
[FONT=Courier New]    Application.DisplayAlerts = True<o:p></o:p>[/FONT]
[FONT=Courier New]End Sub[/FONT]
 
Upvote 0
So it's not workbooks you are trying to download?

I can't see how Excel could download other types of files.
 
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

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