PDA

View Full Version : Download images from the excel and save to a Folder



mohanraj.610
04-28-2016, 01:02 AM
Hi,

I need to multiple download images from the link in excel and save to a folder name given and also rename the images.

Can anyone help.

Thanks in advance!!

GTO
04-28-2016, 03:53 AM
I think you should be able to use the method Here (http://www.vbaexpress.com/forum/showthread.php?29333-Solved-VB-control-of-IE-Dialog-box&p=199691&viewfull=1#post199691) shown by Aaron.

I did not include additional code to find the bottom row with data, but by simple example, try:

In a Standard Module:



Option Explicit
Private Declare Function URLDownloadToFileA Lib "urlmon" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Private Sub Example()
Dim n As Long

For n = 2 To 3
'From All API; example by Matthew Gates (Puff0rz@hotmail.com)
DownloadFile Sheet1.Range(CStr("C" & n)).Value, ThisWorkbook.Path & "\" & Mid$(Sheet1.Range(CStr("C" & n)).Value, InStrRev(Sheet1.Range(CStr("C" & n)).Value, "/") + 1)
Next
End Sub

Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function



Please include whether this will be run in Excel installed in 64-bit mode; as we'd want to change the declared function a bit (probably)...

Hope that helps,

Mark

mohanraj.610
05-03-2016, 04:55 AM
Hi,

It is not working

Kenneth Hobs
05-03-2016, 07:24 AM
Welcome to the forum as Mark said.

From your file, I don't see a valid Drive:\path to poke the file into so Mark set a valid one, that workbook's.

When you say not working, that does not work for me. Please say what does not work. I suspect that it did work.

When troubleshooting, Debug.Print is a good tool. It puts results into the Immediate window. Select the View mode in the Visual Basic Editor (VBE) to select it if needed. Navigate to the build folder to see if it did "work".

Note that Mark used an API. Those should always go to the top of a Module after Option lines of code. He also used Option Explicit as the first line of code at the very top which I highly recommend.



Private Declare Function URLDownloadToFileA Lib "urlmon" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Private Sub Example()
Dim n As Long

For n = 2 To 3
'DownloadFile Sheet1.Range(CStr("C" & n)).Value, ThisWorkbook.Path & "\" & Mid$(Sheet1.Range(CStr("C" & n)).Value, InStrRev(Sheet1.Range(CStr("C" & n)).Value, "/") + 1)
Debug.Print Sheet1.Range(CStr("C" & n)).Value, ThisWorkbook.Path & "\" & Mid$(Sheet1.Range(CStr("C" & n)).Value, InStrRev(Sheet1.Range(CStr("C" & n)).Value, "/") + 1)
Next
End Sub

Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function

prasadrao
05-21-2016, 02:09 AM
sir,
How to extract data between two dates using vba with code?

Thanks for early reply.
Prasadrao.