【问题标题】:Downloading Images from URL and Renaming从 URL 下载图像并重命名
【发布时间】:2017-06-30 07:30:57
【问题描述】:

我有一个 2 列的 Excel 表,A 和 B。A 列有名称,B 列有图片 URL。

我想下载所有图像并将它们重命名为 A 列中的内容。我在这里搜索过,似乎有以前的解决方案,但代码在我的 excel/ 版本上不起作用PC 时出现错误:

"编译错误

必须更新项目中的代码才能在 64 位系统上使用。请查看并更新 Declare 语句,然后用 PtrSafe 属性标记它们。

这是之前的帖子:GET pictures from a url and then rename the picture

将不胜感激并乐于为此提供任何帮助!

【问题讨论】:

  • @Amorpheuses:替换你的“64 位机器,安装最新版本的office”。使用“安装了最新 32 位版本的 office 的 64 位机器。”
  • 等等,有没有可能我为我的 Windows 版本安装了错误的 office 版本?
  • 我不会将 64 位 Office 版本称为 64 位 Windows 系统的错误版本。但显式使用Declarestatements 与 32 位版本不同。见Compatibility Between the 32-bit and 64-bit Versions of Office。 @Amorpheuses 曾表示它适用于他使用 64 位 Windows。但这只有在他在 64 位 Windows 中运行 32 位 Office 时才是正确的。
  • 我提供了一个不需要系统声明的Sub。所以这应该与使用 32 位还是 64 位 Office 无关。

标签: vba excel


【解决方案1】:

下面的Sub 应该和GET pictures from a url and then rename the picture 中的一样。但由于它不使用系统函数,只使用原生 Excel VBA,所以应该与使用 32 位还是 64 位 Office 无关。

Sheet1

代码:

Const FolderName As String = "P:\Test\"

Sub downloadJPGImages()

 Set ws = ActiveWorkbook.Sheets("Sheet1")
 lLastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

 Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
 Set oBinaryStream = CreateObject("ADODB.Stream")
 adTypeBinary = 1
 oBinaryStream.Type = adTypeBinary

 For i = 2 To lLastRow
  sPath = FolderName & ws.Range("A" & i).Value & ".jpg"
  sURI = ws.Range("B" & i).Value

  On Error GoTo HTTPError
  oXMLHTTP.Open "GET", sURI, False
  oXMLHTTP.Send
  aBytes = oXMLHTTP.responsebody
  On Error GoTo 0

  oBinaryStream.Open
  oBinaryStream.Write aBytes
  adSaveCreateOverWrite = 2
  oBinaryStream.SaveToFile sPath, adSaveCreateOverWrite
  oBinaryStream.Close

  ws.Range("C" & i).Value = "File successfully downloaded as JPG"

NextRow:
 Next

 Exit Sub

HTTPError:
 ws.Range("C" & i).Value = "Unable to download the file"
 Resume NextRow

End Sub

【讨论】:

    猜你喜欢
    • 2016-11-14
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多