【发布时间】:2017-02-19 10:46:22
【问题描述】:
我有一个 excel 文件,其中包含文件夹名称 (col A)、图片名称 (col B) 和超链接 (col C) p>
文件夹名称图片名称网址
文件夹 1 图像 1 超链接 1
文件夹2图片2超链接2
文件夹 3 图像 3 超链接 3
我找到了这段代码:
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
它将文件下载到 C:\TMP\ 但我希望它连续下载每个文件到相应的文件夹 (col A)
【问题讨论】:
-
一行中只有一个超链接/文件/文件夹?
-
是的,一个文件,一个文件夹。它应该是连续 4 个超链接,但一个也可以做到这一点。