0

I'm trying to run a macro in Excel 64-bit that downloads an image file linked in cell C2 with the file name in cell B2, and spits it out onto my desktop. But the three lines of code for the URLDownloadToFile function at the top are highlighted in red in the VBA editor. Why isn't excel allowing me to run that URLDownloadToFile function?

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

Sub getJPGfromweb()
    Dim strJPGLink As String
    Dim strJPGFile As String
    Dim Result As Boolean
    strJPGLink = ActiveSheet.Range("C2").Value
    strJPGFile = ActiveSheet.Range("B2").Value
    Result = DownloadFile(strJPGLink, strJPGFile)
    MsgBox Result
End Sub

Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    Dim lngRetVal As Long
    lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    If lngRetVal = 0 Then DownloadFile = True
End Function
shampouya
  • 386
  • 1
  • 6
  • 24
  • I just pasted them into the VBE and looks fine. – SJR Jun 09 '22 at 07:38
  • I believe the first and last arguments are pointers, so should be `LongPtr` not `Long`. – Rory Jun 09 '22 at 08:05
  • @braX thats only half the way. See link above the quesiton. – Pᴇʜ Jun 09 '22 at 08:05
  • Whenever I try to use the Private Declare PtrSafe Function URLDownloadToFile code I get an error that says "Compile Error: Only comments may appear after End Sub, End Function, or End Property". Why is that? – shampouya Jun 09 '22 at 08:53

0 Answers0