从excel链接下载照片

时间:2017-09-29 18:12:08

标签: excel vba

我有一个庞大的excel文件,它有大量的图像超链接。我的客户当然希望将这些图像放在压缩文件夹中。

之前我从未接触过VB代码所以我不知道我在做什么。我有这个,但我不确定要放入'范围'是什么...因为我再也没有经验。一些帮助将不胜感激。我一直收到“运行时错误9”

 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\person\Desktop\images"

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 = 4 To LastRow '<~~ 2 because row 1 has headers
        strPath = FolderName & ws.Range("A" & i).Value & ".jpg"

        Ret = URLDownloadToFile(0, ws.Range("B" & i).Value, strPath, 0, 0)

        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

enter image description here

和那种数据的样本。我只想把图像拉出来 enter image description here

0 个答案:

没有答案