【问题标题】:Inserting an Online Picture to Excel with VBA使用 VBA 将在线图片插入 Excel
【发布时间】:2013-04-19 21:37:29
【问题描述】:

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

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

同样,我使用的系统是 URL 在一列中,即:

xxxx.com/xxxx1.jpg

xxxx.com/xxxx2.jpg

xxxx.com/xxxx3.jpg

xxxx.com/xxxx4.jpg

通过一些搜索,我发现它可以链接到我的 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

【问题讨论】:

  • 为我工作...当您收到错误消息时,filenam 的值是多少?
  • 我最近回答了一个非常相似的问题。让我看看能不能找到。

标签: vba excel spreadsheet


【解决方案1】:

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

Excel VBA Insert Images From Image Name in Column

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

【讨论】:

  • 大卫,感谢您的帮助!现在一切似乎都在工作。虽然有点尴尬,但看起来主要组件缺少的是用于检查任何错误的代码: On Error Resume Next 之后,看起来一切运行顺利。这里唯一的障碍是,如果我运行超过 200 行,excel 就会冻结。不过,这是一种快速的解决方法,可能是我的计算机设置有问题。
  • On Error Resume Next 应该避免使用。更好的做法是捕获潜在异常的错误和代码,而不是简单地忽略它们,这就是该语句的作用。
【解决方案2】:

我知道这个帖子已经有 5 年历史了,但我只想说它真的对我的项目有帮助。 我正在使用 VBA 从订单数据库中引入数据。当我从这些结果中单击订单时,它会显示有关订单的更多详细信息,包括图像 URL。 我遇到的问题是上面的代码旨在添加图像来代替 URL。我想用新查询中的图像替换上一个查询中的图像。经过一些调整后,我让它工作了,但它只是在旧图像之上放置了一个新图像。随着时间的推移,我的 Excel 文件可能会变得非常大,所以这是我的解决方案。我现在唯一的问题是它删除了我放在工作表上的公司徽标。可能有一种更具选择性的方法,或者我可以更改程序以在每次删除图片时从工作簿的另一张表中插入徽标,但这似乎有点俗气。

Sub InsertPic()

Dim productImageUrl As String
Dim productImage As Picture     'Declare image picture object
Dim productImageUrlRng As Range 'Declare range object to contain image URL
Dim productImageRng As Range    'Location image will be placed
'Delete any existing pictures:


Set productImageRng = ActiveSheet.Range("J1:J15") 'Where I want to put the image
Set productImageUrlRng = Range("BA2")  'Cell containing image URL
productImageUrl = productImageUrlRng

productImageRng.Select
'productImageRng.Delete --Does not delete pictures in range
ActiveSheet.Pictures.Delete     'Delete existing images
Set productImage = ActiveSheet.Pictures.Insert(productImageUrl)

With productImage
    .ShapeRange.LockAspectRatio = msoTrue
    '.Width = productImageRng.Width
    .Height = productImageRng.Height
    ' .Top = Rows(cl.Row).Top
    ' .Left = Columns(cl.Column).Left
End With
End Sub

【讨论】:

猜你喜欢
  • 2020-07-02
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多