VBA Download pdf from web

twelin

New Member
Joined
May 10, 2019
Messages
15
Hi,
I have a strange problem. This macro only works for the URLs I have previously visited, but not for "new ones".
Does anyone have a reasonable explanation?

What I want to achieve is:
Fetch a large number of pdf:s. The addresses are listed in Excel.

Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFundName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr) As LongPtr
#Else
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFundName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
#End If



Sub TestDownloadAllEnglishFundRules()

Dim wb As Workbook
Dim ws As Worksheet
Dim i, k, LastRow As Integer
Dim FundName, FileName As String
Dim FileURL As String
Dim DestinationFile As String

Set wb = Workbooks("TestDownload.xlsm")
Set ws = Worksheets("Sheet1")

ws.Select
Range("A1").Select
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

On Error Resume Next

For i = 2 To LastRow
FundName = ws.Cells(i, 2)
'e.g. "Handelsbanken_Amerika_Tema"
FileURL = ws.Cells(i, 3)
'e.g. "https://secure.msse.se/shb/temp/pdf/Handelsbanken_Amerika_Tema_Fund_rules.pdf"
FileName = FundName & "_Fund_rules.pdf"
'The destination file name (i.e. what the downloaded file will be called)
DestinationFile = "C:\Temp\" & FileName

If URLDownloadToFile(0, FileURL, DestinationFile, 0, 0) = 0 Then
Debug.Print "Download started"
End If

ws.Cells(i, 4) = FileURL
'print the web address
Next i

End Sub

Here is an extract from the list in Excel:
ISIN (Huvudklass)FundNameFileURL
SE0011336908Handelsbanken_Amerika_Smabolag_Tema
SE0000355828Handelsbanken_Amerika_Tema
SE0000356073Handelsbanken_Asien_Tema
SE0011090323Handelsbanken_Auto_100_Criteria
SE0011090299Handelsbanken_Auto_25_Criteria
SE0011090307Handelsbanken_Auto_50_Criteria
SE0011090315Handelsbanken_Auto_75_Criteria
SE0000356271Handelsbanken_EMEA_Tema
SE0011338409Handelsbanken_Emerging_Markets_Index
SE0008014914Handelsbanken_Euro_Corporate_Bond_Fund
SE0005991445Handelsbanken_Euro_Obligation
SE0005965696Handelsbanken_Euro_Ranta
SE0000624421Handelsbanken_Europa_Index_Criteria
SE0005936945Handelsbanken_Europa_Selektiv
SE0000396574Handelsbanken_Europa_Tema
SE0005933033Handelsbanken_Finland
SE0005569316Handelsbanken_Flexibel_Ranta
SE0005993128Handelsbanken_Foretagsobligation
SE0007820584Handelsbanken_Foretagsobligation_Investment_Grade
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

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