如何下载文件夹中A列的所有链接?

时间:2013-02-03 18:35:22

标签: html excel vba excel-vba excel-2010

  

可能重复:
  GET pictures from a url and then rename the picture

我需要下载超过30个以上的文件链接。 有没有办法做这个excel?

我想在excel中做,因为要获得那些30多个链接,我必须做一些清理,我在excel中做。

我每天都需要这样做。如果在excel中有办法做的话会很棒。

例如,如果A2是图像,则将此图像下载到文件夹

https://www.google.com/images/srpr/logo3w.png

如果有办法将logo3w.png重命名为B2中的任何内容,那将更加精彩,所以我不必重命名文件。

下面的脚本,我在网上发现,它有效,但我需要帮助重命名它 在A2栏中:向下我有所有链接
在B2栏中:向下我有扩展名为

的文件名

Const TargetFolder =“C:\ Temp \”

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 Test()
For Each Hyperlink In ActiveSheet.Hyperlinks
    For N = Len(Hyperlink.Address) To 1 Step -1
        If Mid(Hyperlink.Address, N, 1) <> "/" Then
            LocalFileName = Mid(Hyperlink.Address, N, 1) & LocalFileName
        Else
            Exit For
        End If
    Next N
    Call HTTPDownloadFile(Hyperlink.Address, TargetFolder & LocalFileName)
Next Hyperlink
End Sub


Sub HTTPDownloadFile(ByVal URL As String, ByVal LocalFileName As String)
Dim Res As Long
On Error Resume Next
Kill LocalFileName
On Error GoTo 0
Res = URLDownloadToFile(0&, URL, LocalFileName, 0&, 0&)
End Sub

2 个答案:

答案 0 :(得分:1)

我很确定您可以稍微修改以下代码以满足您的需求:

Sub DownloadCSV()

Dim myURL As String
myURL = "http://pic.dhe.ibm.com/infocenter/tivihelp/v41r1/topic/com.ibm.ismsaas.doc/reference/LicenseImportSample.csv"

Dim WinHTTPReq As Object
Set WinHTTPReq = CreateObject("Microsoft.XMLHTTP")
Call WinHTTPReq.Open("GET", myURL, False)
WinHTTPReq.send

If WinHTTPReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHTTPReq.responseBody
    oStream.SaveToFile ("D:\DOCUMENTS\timelog.csv")
    oStream.Close
End If

End Sub
祝你好运!

答案 1 :(得分:0)

这对你有用。它将使用列B中的文件名下载和重命名。我只是用一行替换了第二个for循环。 Hyperlink.range.row给出超链接所在的行号。因此,细胞(hyperlink.range.row,2)评估为细胞(1,2),细胞(2,2)等(如果数据在A1,A2,A3 ......中)。假设你在B栏中有扩展名(ex - xyz.png)的文件名,这应该可行。

Const TargetFolder = "C:\Temp\"
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 Test()
   For Each Hyperlink In ActiveSheet.Hyperlinks
       LocalFileName=ActiveSheet.cells(hyperlink.Range.Row,2).value
       Call HTTPDownloadFile(Hyperlink.Address, TargetFolder & LocalFileName)
   Next Hyperlink
End Sub


Sub HTTPDownloadFile(ByVal URL As String, ByVal LocalFileName As String)
   Dim Res As Long
   On Error Resume Next
   Kill LocalFileName
   On Error GoTo 0
   Res = URLDownloadToFile(0&, URL, LocalFileName, 0&, 0&)
End Sub

如果有帮助,请告诉我。