Error Download image by Url Using VBA

amo

Board Regular
Joined
Apr 14, 2020
Messages
141
Office Version
  1. 2010
Platform
  1. Windows
Hello all

I tried to download the image from the URL and it worked normally.
but if it is run on a 64 bit computer then an error message

I don't know what to do, any ideas?
this is my code

VBA Code:
Option Explicit
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
Dim Ret As Long

Sub Download()
   
    
    Dim strPath As String
    Dim FolderName As String
    Dim x As Integer
    Dim i As Long
    Dim sData As Worksheet: Set sData = Sheets("Sheet17")
    
    Application.DisplayAlerts = False
 
    FolderName = "C:\Try"
 
    With sData
    For i = 1 To 100
        For x = 5 To 12
        Application.Calculation = xlCalculationManual
            If Sheet17.Cells(i, x).Value <> "" Then
                strPath = FolderName & "\" & i  & "-" & x - 4 & ".jpg"
                Ret = URLDownloadToFile(0, Sheet17.Cells(i, x).Value, strPath, 0, 0)
            End If
        Next x
    Next i
    Application.Calculation = xlCalculationAutomatic
    End With

    
Application.DisplayAlerts = True
End Sub
 

Attachments

  • Screenshot_1.png
    Screenshot_1.png
    7.7 KB · Views: 18

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try replacing the URLDownloadToFile declaration with:
VBA Code:
#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 Long
#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
 
Upvote 0
Try replacing the URLDownloadToFile declaration with:
VBA Code:
#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 Long
#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
@John_w Thank you
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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