【发布时间】:2019-06-20 17:17:57
【问题描述】:
我正在尝试将图像从 URL 下载到文件夹中。我从对this question 的成功回答中获得了以下代码。
Sub DownloadLinks()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String, strURL As String
Dim c As Range
Set ws = Sheets("Sheet1")
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Set c = ws.Range("BP" & i)
If c.Hyperlinks.Count>0 Then
strPath = FolderName & c.Value & ".jpg"
strURL = c.Hyperlinks(1).Address
Ret = URLDownloadToFile(0, strURL, strPath, 0, 0)
ws.Range("CA" & i).Value = IIf(Ret = 0, _
"File successfully downloaded", _
"Unable to download the file")
Else
ws.Range("CA" & i).Value = "No hyperlink!"
End If
Next i
End Sub
当我运行上述宏时,我得到“编译错误:未定义子或函数”引用 URLDownloadToFile。在其他地方,我看到使用此代码定义的 URLDownloadToFile 会在我将其添加到宏后立即变为红色。
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
将此添加到我的宏的顶部会在第一行出现语法错误。
我是否需要下载一个特殊的补丁或库来运行 URLDownloadToFile?我正在运行 64 位的 Windows 10。还是上面的宏有问题?我没有正确定义 URLDownloadToFile 吗?
【问题讨论】: