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:
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) | FundName | FileURL |
SE0011336908 | Handelsbanken_Amerika_Smabolag_Tema | |
SE0000355828 | Handelsbanken_Amerika_Tema | |
SE0000356073 | Handelsbanken_Asien_Tema | |
SE0011090323 | Handelsbanken_Auto_100_Criteria | |
SE0011090299 | Handelsbanken_Auto_25_Criteria | |
SE0011090307 | Handelsbanken_Auto_50_Criteria | |
SE0011090315 | Handelsbanken_Auto_75_Criteria | |
SE0000356271 | Handelsbanken_EMEA_Tema | |
SE0011338409 | Handelsbanken_Emerging_Markets_Index | |
SE0008014914 | Handelsbanken_Euro_Corporate_Bond_Fund | |
SE0005991445 | Handelsbanken_Eurbligation | |
SE0005965696 | Handelsbanken_Euro_Ranta | |
SE0000624421 | Handelsbanken_Europa_Index_Criteria | |
SE0005936945 | Handelsbanken_Europa_Selektiv | |
SE0000396574 | Handelsbanken_Europa_Tema | |
SE0005933033 | Handelsbanken_Finland | |
SE0005569316 | Handelsbanken_Flexibel_Ranta | |
SE0005993128 | Handelsbanken_Foretagsobligation | |
SE0007820584 | Handelsbanken_Foretagsobligation_Investment_Grade |