vba 从 url 获取图片,然后重命名图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10383614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:04:02  来源:igfitidea点击:

GET pictures from a url and then rename the picture

excelvbaimage

提问by Frank

I have a excel list with a lot of article numbers, eg. "23378847". And I want the pictures of all my article numbers in the list stored in my folder.

我有一个包含很多文章编号的 excel 列表,例如。“23378847”。我想把我所有文章编号的图片存储在我的文件夹中。

But the result will be as under. It should be 23378847.jpg not 152499

但结果将如下。应该是 23378847.jpg 而不是 152499

http://media.byggtjeneste.no/media/bilde/152499/LargeThumbnail
or
http://www.nobb.no/Nobbnr/OrginalBilde/23378847/152499

http://media.byggtjeneste.no/media/bilde/152499/LargeThumbnail

http://www.nobb.no/Nobbnr/OrginalBilde/23378847/152499

Is there a way that I can make a scrips that read my file and save the pic with the same article number as in the list?

有没有一种方法可以让我制作一个读取我的文件并使用与列表中相同的文章编号保存图片的脚本?

回答by Siddharth Rout

Here is a sample which will help you.

这是一个可以帮助您的示例。

I am assuming that your Excel file will look like this. Please amend the code as applicable.

我假设您的 Excel 文件将如下所示。请根据情况修改代码。

enter image description here

在此处输入图片说明

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:\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("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