用VBA插入在线图片到Excel

我目前正在进行一个项目,需要通过URL填充图片单元格。 所有url都在一列中,我想将图片加载到相邻列中。 我不是VBA专家,但是我发现了一些有效的代码,但由于某种原因,我得到一个错误(通常是5张图片)

运行时错误“1004”:无法获取图片类的插入属性

再次,我使用的URL在一个列中的系统,即:

xxxx.com/xxxx1.jpg

xxxx.com/xxxx2.jpg

xxxx.com/xxxx3.jpg

xxxx.com/xxxx4.jpg

通过一些search,我发现它可以链接到我的Excel版本(使用2010),虽然我不完全确定。

这是我正在使用的当前代码:

Sub URLPictureInsert() Dim cell, shp As Shape, target As Range Set Rng = ActiveSheet.Range("a5:a50") ' range with URLs For Each cell In Rng filenam = cell ActiveSheet.Pictures.Insert(filenam).Select Set shp = Selection.ShapeRange.Item(1) With shp .LockAspectRatio = msoTrue .Width = 100 .Height = 100 .Cut End With Cells(cell.Row, cell.Column + 1).PasteSpecial Next End Sub 

任何帮助将非常感激!

原始代码来源: http : //www.mrexcel.com/forum/excel-questions/659968-insert-image-into-cell-url-macro.html

这是我一个月前发布的几乎相同的解决scheme:

Excel VBA从图像名称列中插入图像

 Sub InsertPic() Dim pic As String 'file 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("B1:B7") '<~~ Modify this range as needed. Assumes image link URL in column A. For Each cl In rng pic = cl.Offset(0, -1) Set myPicture = ActiveSheet.Pictures.Insert(pic) ' 'you can play with this to manipulate the size & position of the picture. ' currently this shrinks the picture to fit inside the cell. With myPicture .ShapeRange.LockAspectRatio = msoFalse .Width = cl.Width .Height = cl.Height .Top = Rows(cl.Row).Top .Left = Columns(cl.Column).Left End With ' Next End Sub