情况是这样的:我正在尝试从外部服务器下载一些图片到我的本地计算机上。
Excel 文件有一个图片链接,可以打开并下载图片。
到目前为止我所尝试的是将超链接转换为文本(图片网址)并运行以下代码。
我只是基本熟悉VBA,但对其他语言更熟悉。这是我到目前为止的代码:
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\My Name\Downloads\"
Sub DownloadLinks()
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("B" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("BP" & i).Value & ".jpg"
Ret = URLDownloadToFile(0, ws.Range("BP" & i).Value, strPath, 0, 0)
If Ret = 0 Then
ws.Range("CA" & i).Value = "File successfully downloaded"
Else
ws.Range("CA" & i).Value = "Unable to download the file"
End If
Next i
End Sub
列名称无关紧要,但现在,所有内容都显示为“无法下载文件”,或者如果成功,则它不在我指定的目录中。
有更好的方法来编码吗?
也许与我的数据有关?
如果可能的话,我还希望将文件名保存为另一列中的文本,但这不是必需的。
现在我只需要下载它们。