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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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