Simple VBA help to download data from URL

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello Zack
First of all thank you for your efforts, When I was going through the Thread (as this is exactly my requirement as well) I was stunned to see how much effort you must have put in. It motivates us. I am not a techie nor a coder, but would be thankful if you could tell me how can I get this thread into a real working file? I know bit of a VBA and Macros, but very basic. I saw you mentioning that you uploaded test file due to userform, but I couldn't see it attached. Can you pl share it? Or tell me how to build it?
You may send it to my email (if the forum rules permit the same)

Looking fwd. for your help. Much needed.
 
Upvote 0
Hello Zack
First of all thank you for your efforts, When I was going through the Thread (as this is exactly my requirement as well) I was stunned to see how much effort you must have put in. It motivates us. I am not a techie nor a coder, but would be thankful if you could tell me how can I get this thread into a real working file? I know bit of a VBA and Macros, but very basic. I saw you mentioning that you uploaded test file due to userform, but I couldn't see it attached. Can you pl share it? Or tell me how to build it?
You may send it to my email (if the forum rules permit the same)

PL note I have Excel 2016. 64 bit

Looking fwd. for your help. Much needed.
 
Upvote 0
Hi there,

The file was removed some time ago, after various forum upgrades. While the original file is long gone, and also specific to the OP, I've made a sample file and uploaded to my website. The code, at its base, is fairly straight forward - a one line Windows API call. The result will tell you if it was a success or not, a zero being success.

Sample file link.

The sample file doesn't contain a userform like the OP had, but I do use a Range accessor, which is basically a locally named range with a VBA property so I can access it from intellisense anywhere in the workbook.

Worksheet accessor code (requires sheet/local named ranges to match):
VBA Code:
Public Property Get Range_FilePath() As Range

    Set Range_FilePath = Me.Range("FilePath")

End Property

Public Property Get Range_FileURL() As Range

    Set Range_FileURL = Me.Range("FileURL")

End Property

The code to download the file (all of this is in the sample file) consists of:
  • Windows API (both 32- and 64-bit versions)
  • Test routine
  • Download function
  • Supporting functions
Here is the code:
VBA Code:
Option Explicit


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


Public Sub Test_DownloadInternetFile()
    Debug.Print DownloadInternetFile( _
                FileURL:=wTest.Range_FileURL.Value, _
                FilePath:=wTest.Range_FilePath.Value, _
                NotifyUser:=True)
End Sub


Public Function DownloadInternetFile( _
       ByVal FileURL As String, _
       ByVal FilePath As String, _
       Optional ByVal NotifyUser As Boolean = False _
       ) As Boolean

    Dim DownloadFile As Boolean
    Dim FileReplaced As Boolean

    If ExistingFile(FilePath) Then
        FileReplaced = DeleteFile(FilePath)
        If Not FileReplaced Then
            MsgBox "File already exists. Could not delete it.", vbExclamation + vbOKOnly
            Exit Function
        End If
    End If
    
    DownloadFile = URLDownloadToFile(0, FileURL, FilePath, 0, 0) = 0

    If NotifyUser Then
        If DownloadFile Then
            If FileReplaced Then
                MsgBox "File successfully downloaded and replaced to '" & FilePath & "'.", vbInformation + vbOKOnly
            Else
                MsgBox "File successfully downloaded to '" & FilePath & "'.", vbInformation + vbOKOnly
            End If
        Else
            MsgBox "Unable to download file. Check your URL and file save name.", vbCritical + vbOKOnly
        End If
    End If

End Function


Public Function DeleteFile( _
       ByVal FilePath As String _
       ) As Boolean

    On Error Resume Next
    If ExistingFile(FilePath) Then Kill FilePath
    If Err.Number = 0 Then DeleteFile = True
    Err.Clear

End Function


Public Function ExistingFile( _
       ByVal FilePath As String _
       ) As Boolean

    Dim Attributes As Integer

    On Error Resume Next
    Attributes = GetAttr(FilePath)
    ExistingFile = (Err.Number = 0) And (Attributes And vbDirectory) = 0
    Err.Clear

End Function

Change the cell values to whatever you want, then run Test_DownloadInternetFile.

HTH
 
Upvote 0
Hi there,

The file was removed some time ago, after various forum upgrades. While the original file is long gone, and also specific to the OP, I've made a sample file and uploaded to my website. The code, at its base, is fairly straight forward - a one line Windows API call. The result will tell you if it was a success or not, a zero being success.

Sample file link.

The sample file doesn't contain a userform like the OP had, but I do use a Range accessor, which is basically a locally named range with a VBA property so I can access it from intellisense anywhere in the workbook.

Worksheet accessor code (requires sheet/local named ranges to match):
VBA Code:
Public Property Get Range_FilePath() As Range

    Set Range_FilePath = Me.Range("FilePath")

End Property

Public Property Get Range_FileURL() As Range

    Set Range_FileURL = Me.Range("FileURL")

End Property

The code to download the file (all of this is in the sample file) consists of:
  • Windows API (both 32- and 64-bit versions)
  • Test routine
  • Download function
  • Supporting functions
Here is the code:
VBA Code:
Option Explicit


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


Public Sub Test_DownloadInternetFile()
    Debug.Print DownloadInternetFile( _
                FileURL:=wTest.Range_FileURL.Value, _
                FilePath:=wTest.Range_FilePath.Value, _
                NotifyUser:=True)
End Sub


Public Function DownloadInternetFile( _
       ByVal FileURL As String, _
       ByVal FilePath As String, _
       Optional ByVal NotifyUser As Boolean = False _
       ) As Boolean

    Dim DownloadFile As Boolean
    Dim FileReplaced As Boolean

    If ExistingFile(FilePath) Then
        FileReplaced = DeleteFile(FilePath)
        If Not FileReplaced Then
            MsgBox "File already exists. Could not delete it.", vbExclamation + vbOKOnly
            Exit Function
        End If
    End If
   
    DownloadFile = URLDownloadToFile(0, FileURL, FilePath, 0, 0) = 0

    If NotifyUser Then
        If DownloadFile Then
            If FileReplaced Then
                MsgBox "File successfully downloaded and replaced to '" & FilePath & "'.", vbInformation + vbOKOnly
            Else
                MsgBox "File successfully downloaded to '" & FilePath & "'.", vbInformation + vbOKOnly
            End If
        Else
            MsgBox "Unable to download file. Check your URL and file save name.", vbCritical + vbOKOnly
        End If
    End If

End Function


Public Function DeleteFile( _
       ByVal FilePath As String _
       ) As Boolean

    On Error Resume Next
    If ExistingFile(FilePath) Then Kill FilePath
    If Err.Number = 0 Then DeleteFile = True
    Err.Clear

End Function


Public Function ExistingFile( _
       ByVal FilePath As String _
       ) As Boolean

    Dim Attributes As Integer

    On Error Resume Next
    Attributes = GetAttr(FilePath)
    ExistingFile = (Err.Number = 0) And (Attributes And vbDirectory) = 0
    Err.Clear

End Function

Change the cell values to whatever you want, then run Test_DownloadInternetFile.

HTH
OMG! You are Awesome, never expected such a lightning reply and to be specific much needed Support. many Thanks. I will read through this and comeback if I face any issues.
Once again thanks a ton for your support.
 
Upvote 0
Hello Zack

I went through the File and Code, but actually It is not what I am looking for, I am actually looking for exactly the same specifications that you originally helped OP with. Same. I tried to create a module with code described over time in this thread. But when running it is not working, If it is possible for you then can you guide me on what steps I need to take to build it the same way?

Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,225,135
Messages
6,183,065
Members
453,147
Latest member
Lacey D

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