• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

FIle Format not supported after running below code to down image files from SharePoint link

Rajesh Sonawane

New Member
1749102368463.png

>>> use code - tags <<<
Code:
Option Explicit

Private Declare PtrSafe 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

'~~> This is where the images will be saved. Change as applicable
Const ParentFolderName As String = "D:\DSR Visibility tracker\"

Sub Download()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim Folderpath, strPath As String

    Set ws = Sheets("Sheet1")

    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

    For i = 1 To LastRow
   
        Folderpath = ParentFolderName & ws.Range("A" & i).Value & "\"
   
        If Len(Dir(Folderpath, vbDirectory)) = 0 Then
            MkDir Folderpath
        End If
   
        strPath = Folderpath & "File" & i & ".jpg"
        Ret = URLDownloadToFile(0, ws.Range("B" & i).Value, strPath, 0, 0)

        If Ret = 0 Then
            ws.Range("C" & i).Value = "File successfully downloaded"
        Else
            ws.Range("C" & i).Value = "Unable to download the file"
        End If
 
    Next i

End Sub
 
Last edited by a moderator:
Have You verified - what are value for strPath?
... i seems to be value from 1 to LastRow
What kind of name those files have?
 
As URLDownloadToFile works only with directly downloadable files and fails with other …​
 
Back
Top