How to download a images from url with authentication - vba

I have a code by which i can download a images from non authentication websites URL. It work fine with those websites, but when i try to download a image by url, website like dropbox. It gives me an error.
Now what i want is this, i want a code by which i can download a images from authenticated and non authenticated website url.
Below is my 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
'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "C:\Users\INTEL\Desktop\Hari\Images\"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
'~~> Name of the sheet which has the list
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("A" & i).Value & ".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

If your url's are highlighted in blue click on the first one. Excel should open a window where you can authenticate the connection. Enter your username and password for the server and tick checkbox below to save credentials.
If your url's are not highlighted add column next to it with =url(a1) and then click on it.
Once excel remebers your credentials you can execute your script.

Related

Error Running Vba Code

#If Win64 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByRef pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserve As Long, _
ByRef lpfnCB As LongPtr) _
As LongPtr
#Else
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByRef pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserve As Long, _
ByRef lpfnCB As Long) _
As Long
#End If
Dim Ret As Long
'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "C:\Temp"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
'~~> Name of the sheet which has the list
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("A" & i).Value & ".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
EndSub
enter image description here
Error is given below:
After correcting the parameter in the API function, this is what I used.
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
Const FolderName As String = "C:\Temp\"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("A" & i).Value & ".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
To have the file path if the download was successfull, try it.
So if the file is downloaded successfully the path will be placed in column C and the msg "File successfully downloaded" in column D otherwise column C will contain "Unable to download the file".
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("A" & i).Value & ".jpg"
Ret = URLDownloadToFile(0, ws.Range("B" & i).Value, strPath, 0, 0)
If Ret = 0 Then
ws.Range("C" & i).Value = strPath
ws.Range("D" & i).Value = "File successfully downloaded"
Else
ws.Range("C" & i).Value = "Unable to download the file"
End If
Next i
This worked for me. For more info have a look e.g. here.
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
Const FolderName As String = "C:\Temp"
Sub Sample()
Dim Ret As Long
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
Dim urlFileName As String
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
strPath = FolderName & "\" & ws.Range("A" & i).Value & ".jpg"
urlFileName = ws.Range("B" & i).Value
Ret = URLDownloadToFile(0, urlFileName, 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
Data on Sheet
HeaderA | HeaderB
SomeImage1 | http://www.someaddress.com/Imgs/SomeImage1.jpg

Download data from hyperlinks into creating new folders using vba

Image of data in excelI am downloading some data from net using hyperlinks and to put downloaded data into folders created with names listed in A column.
Right now data is successfully downloaded when there is only one hyperlink for one folder, but now I also wants to put more than 2 files data into same folder.
Can anyone suggest a way to enhance the code to allow that?
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
'> This is where the files will be saved. Change as applicable
Const FolderName As String = "C:\Users\a3rgcw\Downloads\"
Sub Download()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim strPath As String
Set ws = Sheets("Sheet1")
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastRow
strPath = FolderName & ws.Range("A" & i).Value & ".zip"
ret = URLDownloadToFile(0, ws.Range("D" & i).Value, strPath, 0, 0)
If ret = 0 Then
ws.Range("F" & i).Value = "PR data successfully downloaded"
Else
ws.Range("F" & i).Value = "Unable to download PR data"
End If
Next i
End Sub
edited after OP clarifications he doesn't have hyperlinks
as per your shown code and link, your code doesn't actually create new folders, rather it creates many new files in "C:\Users\a3rgcw\Downloads\" folder (i.e. your FolderName variable
and since those files names are built with ws.Range("A" & i).Value & ".zip", then for every same value in any column A cell it overwrites the existing file with the new one
furthermore your link shows column "C" with hyperlinks while your code read them from column "D" (ws.Range("D" & i).Value
to avoid files overwriting you could define zip name out of a combination of "folder" name (from column A cells) and file name (from corresponding hyperlink address) like follows (assuming your code assumption for hyperlinks column is the valid one)
Sub Download()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).row
For i = 1 To LastRow
strPath = FolderName & _
ws.Range("A" & i).Value & "-" & _
GetName(ws.Range("D" & i)) & ".zip"
ret = URLDownloadToFile(0, ws.Range("D" & i).Value, strPath, 0, 0)
If ret = 0 Then
ws.Range("F" & i).Value = "PR data successfully downloaded"
Else
ws.Range("F" & i).Value = "Unable to download PR data"
End If
Next i
End Sub
Function GetName(rng As Range) As String
With rng
GetName = Right(.Value, Len(.Value) - InStrRev(.Value, "/"))
End With
End Function
which could also be refactored as follows:
Sub Download()
Dim strPath As String
Dim cell As Range
With Sheets("Sheet1")
For Each cell In .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
strPath = FolderName & _
cell.Value & "-" & _
GetName(cell.Offset(, 3)) & ".zip"
ret = URLDownloadToFile(0, cell.Offset(, 3).Value, strPath, 0, 0)
cell.Offset(, 5).Value = IIf(ret = 0, "PR data successfully downloaded", "Unable to download PR data")
Next
End With
End Sub
Function GetName(rng As Range) As String
With rng
GetName = Right(.Value, Len(.Value) - InStrRev(.Value, "/"))
End With
End Function

Excel downloading pictures from hyperlink

I am currently using this code using the developer console to try to mass download all of the images off the hyperlinks I have in my excel document.
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
'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "C:\Temp\"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
'~~> Name of the sheet which has the list
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("A" & i).Value & ".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
It seems that it will download the images if the pic name column I have in column A is something like this - "calculator.jpg"
However, I would like the picture name to be the hyperlink I am using to download the images, aka something like www.hyperlink.com/calculator.jpg
My code seems to not download the images when the hyperlink is in the picture name column, even though it does print in column C "download successful."
If someone could help me I would really appreciate it!
However, I would like the picture name to be the hyperlink I am using to download the images, aka something like www.hyperlink.com/calculator.jpg
Like I said in the comments above, "You can't have the pic name as hyperlink as "/" is not a valid character for a file name. You have to extract "calculator.jpg" from col A and then use it in the code"
Is this what you are trying?
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
'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "D:\Temp\"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
Dim MyAr
'~~> Name of the sheet which has the list
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
MyAr = Split(ws.Range("A" & i).Value, "/")
'~~> C:\Temp\Calculator.jpg
strPath = FolderName & MyAr(UBound(MyAr))
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

Download pictures from hyperlink to specific folder

I have an excel file with folder names(col A), picture names (col B) and hyperlinks (col C) I want to download pictures to a disc from hyperlinks to specific folder (indicated in col A).
FolderName ImageName URL
folder1 image1 hyperlink 1
folder2 image2 hyperlink 2
folder3 image3 hyperlink 3
I have found this 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 FolderName As String = "c:\TEMP\"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
'~~> Name of the sheet which has the list
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("B" & i).Value & ".jpg"
Ret = URLDownloadToFile(0, ws.Range("C" & i).Value, strPath, 0, 0)
If Len(Dir(FolderName, vbDirectory)) = 0 Then
MkDir FolderName
End If
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
It download the files to C:\TMP\ but I would like it to download each file in a row to corresponding folder (col A)
That is quite simple.
Since you are using a CONSTANT as a saving directory Const FolderName As String = "c:\TEMP\" you are not going far if you copy-paste your code insde your workbook.
You should try first of all to understand how the code works and give it a try, but anyway...
Without inserting the Const line, you have to Dim a variable that is going to contain a string of your directory and that will change every time you change row. Basically here:
For i = 2 To LastRow
FolderName = ws.Range("A" & i).text ' this is how you get the folder name from column "A" every line
strPath = FolderName & ws.Range("B" & i).Value & ".jpg"
Ret = URLDownloadToFile(0, ws.Range("C" & i).Value, strPath, 0, 0)
If Len(Dir(FolderName, vbDirectory)) = 0 Then
MkDir FolderName
End If
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

Download Files (PDF) from Web

I am fresh-starter at VBA.
How do I download PDF file using UrlDownloadToFile from http://cetatenie.just.ro/wp-content/uploads/?
Can anybody help with this? The code is searching the PDF files udner hyperlinks and matches them under some criteria, i.e. the current year under their name.
Function UrlDownloadToFile(lNum As Long, sUrl As String, sPath As String, _
lNum1 As Long, lNum2 As Long) As Long
UrlDownloadToFile = 0
End Function
Sub DownPDF()
' This macro downloads the pdf file from webpage
' Need to download MSXML2 and MSHTML parsers and install
Dim sUrl As String
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Dim hAnchor As MSHTML.HTMLAnchorElement
Dim Ret As Long
Dim sPath As String
Dim i As Long
sPath = "C:\Documents and Settings\ee28118\Desktop\"
sUrl = "http://cetatenie.just.ro/wp-content/uploads/"
'Get the directory listing
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "GET", sUrl
xHttp.send
'Wait for the page to load
Do Until xHttp.readyState = 4
DoEvents
Loop
'Put the page in an HTML document
Set hDoc = New MSHTML.HTMLDocument
hDoc.body.innerHTML = xHttp.responseText
'Loop through the hyperlinks on the directory listing
For i = 0 To hDoc.getElementsByTagName("a").Length - 1
Set hAnchor = hDoc.getElementsByTagName("a").Item(i)
'test the pathname to see if it matches your pattern
If hAnchor.pathname Like "Ordin-*.2013.pdf" Then
Ret = UrlDownloadToFile(0, sUrl & hAnchor.pathname, sPath, 0, 0)
If Ret = 0 Then
Debug.Print sUrl & hAnchor.pathname & " downloaded to " & sPath
Else
Debug.Print sUrl & hAnchor.pathname & " not downloaded"
End If
End If
Next i
End Sub
Sorry - I should have guessed that URLDownloadToFile was an API call and could have answered the whole question at SQL "%" equivalent in VBA.
Remove the function named URLDownloadToFile completely. Paste this at the top of the module where your Sample procedure is
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
Now change that one line in Sample to look like this
Ret = URLDownloadToFile(0, sUrl & hAnchor.pathname, sPath & hAnchor.pathname, 0, 0)
Then you should be good to go. If you want some different file name, then you'll have to code some logic to change it at each iteration.