VBA 使用相邻单元格中的 url 下载并嵌入图像
VBA download and embed images using url from adjacent cell
我尝试了以下解决方案。
Inserting an Online Picture to Excel with VBA
不幸的是,我收到 运行 次错误“1004”
"Unable to get the Insert property of the Picture class"
在以下代码处停止:
Set myPicture = ActiveSheet.Pictures.Insert(pic)
这可能是因为我的 Office 版本 2016(64 位)?
如果没有,是否有关于如何使用 AJ 列中的图像 URL 将图像嵌入到 AK 列中的相邻单元格的任何建议?
提前致谢
some evidence Excel 从 AWS 下载时遇到问题,我已经使用您提到的 URL 重现了您的问题。在这种情况下,如果我在最后期限前,我会在第一个方法失败时采用这种后备方法:下载文件,然后将其插入文档,然后删除文件。
directDownloadFailed:
Dim FileNum As Long
Dim TempFile As String
Dim FileData() As Byte
Dim WHTTP As Object
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
WHTTP.Open "GET", imgURL, False
WHTTP.Send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
FileNum = FreeFile
TempFile = "path\to\save\img.jpg"
Open TempFile For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
Set img = ActiveSheet.Pictures.Insert(TempFile)
SetAttr TempFile, vbNormal
Kill TempFile
GoTo resumeProcessingImg
@keydemographic
感谢您的回复,这听起来确实是一个选项,但我无法使该代码正常工作或将其合并到我正在使用的代码中。我在 GoTo resumeProcessingImg
上遇到编译错误 "Label Not defined"
以下将下载图像并将其嵌入到单元格中,但代码在到达 s3 aws 图像 url 后停止。
我会尝试其他几种方法来合并您的代码,但到目前为止我运气不太好。
Sub URL2IMG()
Dim pic As String 'path of pic
Dim myPicture As Picture 'embedded pic
Dim rng As Range 'range over which we will iterate
Dim cl As Range 'iterator
Set rng = Range("b2:b12") '<~~ as needed, Modify range to where the images are to be embedded.
For Each cl In rng
pic = cl.Offset(0, -1) '<~~ defines image link URL in column to the left of the embedded column
Set myPicture = ActiveSheet.Pictures.Insert(pic)
'you can play with the following to manipulate the size & position of the picture.
With myPicture
.ShapeRange.LockAspectRatio = msoFalse
' currently this shrinks the picture to fit inside the cell.
.Width = cl.Width
.Height = cl.Height
.Top = Rows(cl.Row).Top
.Left = Columns(cl.Column).Left
End With
Next
End Sub
我尝试了以下解决方案。 Inserting an Online Picture to Excel with VBA
不幸的是,我收到 运行 次错误“1004” "Unable to get the Insert property of the Picture class" 在以下代码处停止:
Set myPicture = ActiveSheet.Pictures.Insert(pic)
这可能是因为我的 Office 版本 2016(64 位)? 如果没有,是否有关于如何使用 AJ 列中的图像 URL 将图像嵌入到 AK 列中的相邻单元格的任何建议?
提前致谢
some evidence Excel 从 AWS 下载时遇到问题,我已经使用您提到的 URL 重现了您的问题。在这种情况下,如果我在最后期限前,我会在第一个方法失败时采用这种后备方法:下载文件,然后将其插入文档,然后删除文件。
directDownloadFailed:
Dim FileNum As Long
Dim TempFile As String
Dim FileData() As Byte
Dim WHTTP As Object
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
WHTTP.Open "GET", imgURL, False
WHTTP.Send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
FileNum = FreeFile
TempFile = "path\to\save\img.jpg"
Open TempFile For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
Set img = ActiveSheet.Pictures.Insert(TempFile)
SetAttr TempFile, vbNormal
Kill TempFile
GoTo resumeProcessingImg
@keydemographic
感谢您的回复,这听起来确实是一个选项,但我无法使该代码正常工作或将其合并到我正在使用的代码中。我在 GoTo resumeProcessingImg
上遇到编译错误 "Label Not defined"以下将下载图像并将其嵌入到单元格中,但代码在到达 s3 aws 图像 url 后停止。 我会尝试其他几种方法来合并您的代码,但到目前为止我运气不太好。
Sub URL2IMG()
Dim pic As String 'path of pic
Dim myPicture As Picture 'embedded pic
Dim rng As Range 'range over which we will iterate
Dim cl As Range 'iterator
Set rng = Range("b2:b12") '<~~ as needed, Modify range to where the images are to be embedded.
For Each cl In rng
pic = cl.Offset(0, -1) '<~~ defines image link URL in column to the left of the embedded column
Set myPicture = ActiveSheet.Pictures.Insert(pic)
'you can play with the following to manipulate the size & position of the picture.
With myPicture
.ShapeRange.LockAspectRatio = msoFalse
' currently this shrinks the picture to fit inside the cell.
.Width = cl.Width
.Height = cl.Height
.Top = Rows(cl.Row).Top
.Left = Columns(cl.Column).Left
End With
Next
End Sub